RadioHead seems to stop working

I have a Nano receiving single characters from an RF 433 transmitter on a Pro Mini working fine. Then thru if statements, the Nano does an analogWrite to three PWM pins 9, 6 & 5 to compose an RGB led's color depending on the character received. And this appears to also work well so long as the analogWrite values are high 150-255. But if say the vales are low eg. R=15 G=7 B=5 (pins 9,6,5) the receiver stops getting any subsequent data. The transmitter is broadcasting ok still. They are the very cheap green 433 rf pair. The receiver is powered on it own using a 5v regulator sharing GND with the nano. Is what I'm trying to do too much for the Nano to cope with or does it sound like I've damaged my Nano somewhere down the line and that is the problem?

Nano is perfectly capable of this and much more besides. It does not sound like your Nano is damaged, because the LEDs do light and data is getting received, just not in all circumstances.

Chances are there's a problem with your circuit or your code or both. But because you have not shared anything about those, no one can help further at this time.

Please read the forum guide in the sticky post at the top of the forum second section. That will tell you some of the things you need to post in order for anyone to help you in more detail.

More likely, RadioHead is using a hardware timer that is in use by analogWrite().

You do have current limiting resistors on the LEDs, right? :slight_smile: Also if you have "spaghetti wiring", RFI from the PWM could be leaking into the receiver.

Please read the forum guidelines again(?) and post complete hardware and software information.

Sure. I just wanted to hear back from you guys as a simple response could have highlighted an obvious oversight or known issue.

Here's my code:

#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
int red = 9;
int green = 6;
int blue = 5;
//
RH_ASK driver(2000, 2, 4, 10); // ESP8266 or ESP32: do not use pin 11 or 2

void setup()
{
    driver.init(); 
    Serial.begin(9600);	  // Debugging only
    Serial.println("Serial OK");
    if (!driver.init())
    Serial.println("init failed");
    pinMode(red,OUTPUT);
    pinMode(green,OUTPUT);
    pinMode(blue,OUTPUT);    
}

void loop()
{
    uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];  

    uint8_t buflen = sizeof(buf);

    if (driver.recv(buf, &buflen)) // Non-blocking
    {

    String rcv="";
 
    for(int i = 0; i < buflen; i++) {
      rcv += (char)buf[i];
    }
    Serial.print("Received: ");
    Serial.println("|" + rcv + "|");
    
 

if (rcv == "M") {
    Serial.print("Received: ");
    Serial.println("Magenta"); 
    analogWrite(red,255); 
    analogWrite(green,0); 
    analogWrite(blue,50); 
}  

if (rcv == "C") {
    Serial.print("Received: ");
    Serial.println("Cyan"); 
    analogWrite(red,0); 
    analogWrite(green,255); 
    analogWrite(blue,255); 
}

if (rcv == "B") {
    Serial.print("Received: ");
    Serial.println("Blue"); 
    analogWrite(red,0); 
    analogWrite(green,0); 
    analogWrite(blue,255); 
} 

if (rcv == "Y") {
    Serial.print("Received: ");
    Serial.println("Yellow"); 
    analogWrite(red,255); 
    analogWrite(green,65); 
    analogWrite(blue,0); 
}  

if (rcv == "R") {
    Serial.print("Received: ");
    Serial.println("Red"); 
    analogWrite(red,255); 
    analogWrite(green,0); 
    analogWrite(blue,0); 
} 

if (rcv == "G") {
    Serial.print("Received: ");
    Serial.println("Green"); 
    analogWrite(red,0); 
    analogWrite(green,255); 
    analogWrite(blue,0); 
}  

if (rcv == "O") {
    Serial.print("Received: ");
    Serial.println("Orange"); 
    analogWrite(red,255); 
    analogWrite(green,15); 
    analogWrite(blue,0); 
} 

if (rcv == "K") {
    Serial.print("Received: ");
    Serial.println("Black"); 
    analogWrite(red,0); 
    analogWrite(green,0); 
    analogWrite(blue,0); 
}  

if (rcv == "W") {
    Serial.print("Received: ");
    Serial.println("White"); 
    analogWrite(red,255); 
    analogWrite(green,140); 
    analogWrite(blue,140); 
} 

if (rcv == "D") {  // if this gets to run, nothing further is received.
    Serial.print("Received: ");
    Serial.println("Dimmed"); 
    analogWrite(red,150); 
    analogWrite(green,150); 
    analogWrite(blue,150); 
} 

    rcv="";
    delay(100);
  }

}

Yes current limiting resistors are on each pin of RGB led.

RadioHead and PWM on pin 9 conflict over Timer1.

The receiver is an xl-ro3a. The resistors from pins 9 6 & 5 are all 220R. When I do away with the receiver and use the serial monitor to send characters all the colors are achieved including the "dimmed" values. There is some "spaghetti" (about 8 breadboard cables). But it ONLY causes the problem when the analogWrite is below 150 on each pin. Could you explain more about the hardware timer as I can't help feeling that RadioHead is the problem.

Thanks I'll look into that. I only just seen your post.

Try replacing pin 9 with 11 or 3. If @jremington's theory is correct, that should fix it. PWM pins 9, 10 use the atmega328's timer1, which may be used by Radiohead library, preventing those pins from working normally as PWM pins. Probably any analogWrite() value under 128 will be off and 128 or above will be on. Pins 11, 3 use the atmega's timer2, so hopefully won't be affected.

Yes, that's done the trick. Thank you [PaulRD] and [jremington]. I just couldn't make sense of it, but then I've only owned any Arduino for about two weeks now, so - lot to learn.
Thanks again.

Pins 11 and 3 are both connected to Timer2, so there should have been no difference at all.

"seemed to" is meaningless.

Oh ok. It did run slower. But, I have three versions of the sketch, could have been me muddling them up, or I disturbed cables at some point.
Thanks again.

That really makes no sense, and without quantitatively documenting the issue or identifying the actual problem, such comments just cause confusion.

Keep in mind that radio is inherently unreliable, and lots of changes in the nearby environment, like where you sit, can affect the reliability and "apparent speed" of data transmission.

You're welcome

To mention someone in a post, type @ and start typing their name. That way, they get notified that you mentioned them.

Like this:
@of_a_lunatic @jremington

It ran slower because I used a slightly different sketch by mistake. @jremington . Thanks for the '@' tip too @PaulRB .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.