2

We try to remote control an elegoo robot car, (https://www.elegoo.com/product/elegoo-uno-project-smart-robot-car-kit-v1-0/) with a RF-433 transmitter and receiver. The car has 4 dc motors, controlled by an L298N controller and the motors are powered by 2 18650 4200mAh 3.7V Li-ion batteries which also power an Arduino Uno. When we try to control the car with PWM through analogWrite without the RF-433 it works fine, but when RF is used motors on one side of the car does not work. We have narrowed down the problem to a specific part of the code:

if (!Radio.init()) {
    Serial.println("init failed");
}

When this part is included the problem occurs. But when commented out the car moves fine. Which is why we think that there is an interference, between the RH_ASK library and the arduino code. The rest of the code is here.

#include < RH_ASK.h >
#include < SPI.h > // Not actually used but needed to compile

RH_ASK Radio(2000, 0);

// Pin connections to L298n motor controller
int in1 = 9;
int in2 = 8;
int in3 = 7;
int in4 = 6;

/*define channel enable output pins*/
int ENA = 10;
int ENB = 5;

/*define forward function*/
void _mForward() {
    analogWrite(ENA, 130);
    analogWrite(ENB, 130);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    Serial.println("Forward");
}

// function to turn right
void _mRight() {
    analogWrite(ENA, 130);
    analogWrite(ENB, 130);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    Serial.println("Right");
}

// Function to stop the car
void _mFreeze() {
    analogWrite(ENA, LOW);
    analogWrite(ENB, LOW);
}

void setup() {
    Serial.begin(9600); // Debugging only
    pinMode(in1, OUTPUT);
    pinMode(in2, OUTPUT);
    pinMode(in3, OUTPUT);
    pinMode(in4, OUTPUT);
    pinMode(ENA, OUTPUT);
    pinMode(ENB, OUTPUT);
    if (!Radio.init()) { // initialize rf communication
        Serial.println("init failed");
    }
}

void loop() {
    _mForward();
    uint8_t buf[RH_ASK_MAX_MESSAGE_LEN]; //Read transmitted message into an array
    uint8_t buflen = sizeof(buf);

    // stop the car if we receive a message
    while (Radio.recv(buf, & buflen)) {
        _mFreeze();
    }
}

Another program runs the transmission and sends a message when a button is pressed.

Ruslan
  • 122
  • 1
  • 14
  • if _mFreeze(); works as intended, I would guess that you dont need radio.init. can you find Radio.init() and share it here ? – nikhil patil Nov 09 '17 at 20:42

1 Answers1

1

The answer to your question is the timers. In order to generate the PWM signal for controlling the motors, Arduino uses hardware timers. There are three of them in the Arduino Uno, and every timer responsible for 2 PWM outputs (6 in total). This topic is well described on Arduino web site: https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM

However, the RH_ASK library also uses timers. From the official documentation:

The RH_ASK driver uses a timer-driven interrupt to generate 8 interrupts per bit period. RH_ASK takes over a timer on Arduino-like platforms. By default it takes over Timer 1. You can force it to use Timer 2 instead by enabling the define RH_ASK_ARDUINO_USE_TIMER2 near the top of RH_ASK.cpp

If two different parts of the program try to control the timer, you get problems. Timer 1 is responsible for generating PWM signal on pins 9 and 10. So to solve your issue you can connect ENA pin to any other available analog output pins (5, 6, 3, 11).

Ruslan
  • 122
  • 1
  • 14