Help with RC car transceiver/receiver code

Greetings,
I am a complete beginner in this field and am in the middle of building a RC Car for a class.
Currently I am stuck in that I cant get my single servo motor to respond to the input from a 10k Pot from its transmitter for steering of the RC car. I am certain that I messed up somewhere along the way in the programming and but it could still possibly be the wiring. Checked the Pot and it works fine and serial monitor values change when pot it is turned . Also the throttle works but for some reason the steering remains unresponsive. The wireless module being used is NRF24L01 with 3.3v adapter. Arduino Uno for receiver and Arduino Nano for transmitter. Here are the pinouts. Thank you in advance!

Receiver:
5v-VCC (NRF24L01)
GND-GND (NRF24L01)
GND-ESC GND
VIN-ESC 5v
A0-SERVO VCC
GND-SERVO GND
13-SCK (NRF24L01)
12-M1 (NRF24L01)
11-MO (NRF24L01)
9- OUTPUT SERVO
8-CSN (NRF24L01)
7-CE (NRF24L01)
3-ESC OUTPUT

Transmitter:
GND- GND of 10k Pot(steering) and 10k Pot(throttle)
7-CE (NRF24L01)
8-CSN (NRF24L01)
11-MO (NRF24L01)
12-M1 (NRF24L01)
GND-GND (NRF24L01)
5V-10K Pot(steering), 10K Pot(throttle), and VCC (NRF24L01)
A1-OUTPUTPot(throttle)
A0-OUTPUT Pot(steering)
13-SCK (NRF24L01)

sorry can only post one picture as new user.

--------------------receiver code----------------

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#define VCC4 A0 //links analog0 with servoVCC
Servo esc;
Servo servo;
RF24 radio(7, 8); // wifi modules pins(CE, CSN) are connected to pins (7, 8)
const byte addresses[][6] = {"node1", "node2"};
byte wewe;
struct MyData {
  int state1;
  int state2;
  int state3;
  int state4;
};
MyData data;
int vel;
void resetData()
{
  data.state1 = 0;
  data.state2 = 0;
  data.state3 = 0;
  data.state4 = 0;
}
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, addresses [0]);
  radio.setPALevel(RF24_PA_MIN);
  servo.attach(9); //command pin of the steering servo is connected to pin9
  esc.attach(3); //esc signal pin connected to pin3
  esc.writeMicroseconds(1000); //initialize the signal to 1000
  pinMode(VCC4, OUTPUT); //sets servoVCC to output
}
void loop() {
  radio.startListening();
  if (radio.available()) {
    radio.read(&data, sizeof(MyData));
  }
  //motor speed control
  if (data.state1 > 710) vel = 900; //710 you will have to change this value, based on your potentiometer signal
  if (data.state1<710 and data.state1>640) vel = 2000 - (data.state1 - 640) * 13; //710 and 640 you will have to change this values, based on your potentiometer signal
  if (data.state1 < 640) vel = 2000; //640 you will have to change this value, based on your potentiometer signal
  esc.writeMicroseconds(vel);
  
  //steering servo control
  if (data.state2<460){ //460 you will have to change this value, based on your potentiometer signal
    wewe=107+(460-data.state2)/12; //107 is the 0 angle of my steering servo
  }
  if (data.state2>540){//540 you will have to change this value, based on your potentiometer signal
    wewe=107-(data.state2-540)/12;
  }
  if (data.state2>460 and data.state2<540) wewe=107;//460 and 540 you will have to change this values, based on your potentiometer signal

  Serial.println (vel);
  servo.write (wewe);
  digitalWrite(VCC4, HIGH); //supplies servoVCC with 5v
}

--------------Transmitter Code---------

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); //wifi module pins (CE, CSN) are connected to pins (D7,D8) of the arduino 
struct MyData {
  int state1;
  int state2;
  int state3;
  int state4;
};
MyData data;
void resetData() 
{
  data.state1 = 0;
  data.state2 = 0;
  data.state3 = 0;
  data.state4 = 0;
}
const byte address[6] = "node1";
void setup() {
  pinMode (14, INPUT); // steering potentiometer output is connected to A0
  pinMode (15, INPUT); // throttle potentiometer output is connected to A1
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  Serial.begin (9600);
}
void loop() {
  radio.stopListening();
  data.state1= analogRead(15); //sends throttle data to receiver 
  data.state2=analogRead(14); //sends steering data to receiver
  Serial.println (data.state1);
  radio.write (&data,sizeof(MyData));
  delay(1);
}

Don't post ANY pictures of code... they are basically useless.

Post any/all code using the code tags in the editor (these things </>).

1 Like

how did you verify the steering pot? i only see a print for state1 which the comment says is for the throttle

have you verified the received values? printing the received data after each treception would eliminate one possible cause

looks like the rx code is re-printing the "vel" instead of the "weve" value

1 Like

You may be having power issues. You should have a separate power source for the servo. I have found that when the servo starts it draws enough current to cause a voltage drop and reset the Arduino. Since you are using A0 it might even damage the board.

2 Likes

Thank you for the advice Todd,
I am used to building much simpler circuits and will try splitting the 5v coming from the LiPo battery/ESC splitter to the arduino/servoVCC. Perhaps even add a capacitor to offset the voltage drop from the servo. At the moment I can hear ticking from it as though its on, just no actual movement from the servo arm itself.

Hello gcjr,
I only verified that the pot is working by testing it with a multimeter and looking for changes in value from the Serial Monitor. I have not actually verified that the receiver is getting the transmission. Unfortunately I do not know how to properly write my own structured code and can only modify or copy/paste.
I will try adding a "Serial.printIn (data.state2);" into the transmitter's loop code and figure out how to re-print the "wewe" variable as well.

do this in your rx code

void loop () {
    radio.startListening ();
    if (radio.available()) {
        radio.read (&data, sizeof(MyData));

        char s [80];
        sprintf (s, " %6d, %6d, %6d, %6d",
            data.state1, data.state2, data.state3, data.state4);
        Serial.println (s);
    }

THANK YOU ALL!
Looks like adding GCJR's loop code to properly run through the states and separating the ServoVCC while adding capacitors where needed as Todd suggested did the trick!
It would've been really embarrassing showing up to class today and be the only one who decided to 3D print their own car as opposed to buying the recommended RC car/Arduino kit only to have it be able to drag race. I really appreciate your guys help, here is the finished product!

Glad it worked out!

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