Arduino RC Car Project
                    Author : Dinesh Kumar Wickramasinghe
                    Introduction and Images
                    
                        Hello Friends. Today I am going to build a remote controlled car using Arduino UNO
                board and some other components. This car has basically four functions that can
                be controlled by the Remote Controller transmitter module. Those are Move forward,
                Turn Right, Turn Left and Reverse.
                    
                    Before we start, here are some images of final version of the built car.
                    
                    What we need to build the RC Car?
                    
                        
                            
                                | Component | Quantity | 
                        
                        
                            
                                | Arduino UNO Rev3 or any compatible version | 1 | 
                            
                                | Gear Motors or DC motors (12V or 9V) | 2 | 
                            
                                | Remote Controller Module eBay Link (If the link is not working, please search on ebay as "IC 2262/2272 4 CH Key Wireless Security")
 | 1 | 
                            
                                | Batteries (12V or 9V) | 1 Pack | 
                            
                                | Small Power Switch | 1 | 
                            
                                | L298 DC Motor Driver IC | 1 | 
                            
                                | IN4148 Diodes | 8 | 
                            
                                | 0.5 Ohm 25W | 2 | 
                            
                                | Hook up wires | Around 10 | 
                        
                    
                    The remote controller module.
                    
                        I purchased this item from one of eBay seller few months ago. It cost me 3.5 USD at that time. This module has
                a transmitter module and a receiver circuit. Transmitter has built in batteries,
                so you do not need to worry about batteries. It has four buttons. That means it
                can be used to control four channels. In our project, we are going to control the
                basic four functions (move forward, turn right and left, reverse) of the remote
                car.
                    
				
				
				
				
				
                    
                    
                        The receiver has seven terminals. Two of them are 5V power inputs. Other are (D0
                to D3) output pins. I am not sure about the UT pin.
                    
                    
                    
                        D0 to D3 pins are related to the four buttons of the transmitter module. For an
                example, when you supply the DC 5V to the receiver module, initially D0 to D3 terminals
                are in digital 0 state. When you press a button of the transmitter module, then
                the corresponding terminal in the receiver change its status from Digital 0 to the
                Digital 1 (5V). For an example in my RC module, when I press the button B on transmitter,
                D0 pin changes to the digital 1 state. Similarly Button D is to D1, A is to D2 and
                Button C is to D3 terminal in my module. I recommend you to identify the relationship
                of transmitter buttons and corresponding terminal of your RC module before connect
                this in to Arduino board.
                    
                    
                        Hope now you have an understanding about the RC module functions.
                    
                    L298 DC Motor Driver
                    
                        To control DC motors based on Arduino signals, I used L298 motor driver IC. It is
                a famous motor driver IC. It can control two DC motors.
                    
                    
                    
                        Following schematic shows the inside configurations of the L298 Integrated circuit.(Please
                click on the image to enlarge)
                    
                    
                        
                    
                    
                        Here it shows only one DC motor connected. Same as that you can connect other motor
                in to OUT3, OUT4 (13, 14) pins of the L298 IC via four IN4148 diodes.
                    
                    
                        Please note that you should connect pin number 4 (Vs) in to the supply voltage.
                For an example if your motors are working on 9V, connect the (+) node of your batteries
                to pin 4. Then the IC will return 9v to the output terminals (2, 3 and 13, 14) based
                on the status of the input terminals (EnA, In1, In2 etc.)
                    
                    
                        Please note that you have to connect IN4148 diodes, 100nf capacitors and 0.5 Ohm
                resistors from outside of the IC. They are not inside the IC. Actually I didn’t
                use 0.5 Ohm resistors in my project. It is not a must. You can connect Pin 1 and
                15 directly to ground. The real purposes of those resistors are to measure the current
                through the motors. Don’t worry about this now.
                    
                    
                        Please see the below image of final version of my L298 based motor driver circuit.
                    
                    
                    
                        Please see the below image of final version of my L298 based motor driver circuit.
                    
                    
                    How to control the car based on RC signals?
                    
                        In my project, I used button A of the RC module to move car forward, Button D to
                reverse, Button C to turn left and Button B to turn the car right.
                    
                    
                        Actually the theory behind this is, when the car is moving forward, right side motor
                rotating to right direction and Left side motor rotating to left direction. When
                the car is reversing, wise verse happens. That means right side motor rotating to
                left direction and Left side motor rotating to right direction.
                    
                    
                        When the car turns right, Right side motor stops running and Left side motor rotating
                to left.
                    
                    
                        When the car turns left, left side motor stops running and right side motor rotating
                to right.
                    
                    
                        Let’s clear this theory using a small diagram. Please see the below diagram to understand
                this.
                    
                    
                    Arduino Connectivity
                    
                        To connect the RC receiver module to Arduino, we have to configure four Arduino
                pins as inputs. In my example, I choose Arduino pin numbers 2,3,4,5 as inputs pins
                to connect the RC reviver module. Other than this, we have to connect 5V and Ground
                pins of the RC receiver module to Arduino power out pins.
                    
                    
                        Please use the following table to understand the connectivity of your RC receiver
                module and Arduino.
                    
                    
                        
                            
                                | RC Transmitter Button | Receiver Terminal | Arduino Connector | 
                        
                        
                            
                                | A | D2 | 2 | 
                            
                                | B | D0 | 3 | 
                            
                                | C | D3 | 4 | 
                            
                                | D | D1 | 5 | 
                        
                    
                    
                        Please see the below image to clear the things
                    
					
					
					
					
					
                    
                    
                        Please make sure to identify your RC transmitter buttons and corresponding receiver
                terminals before you connect your RC receiver module to Arduino. Otherwise you may
                experience wrong functionality.
                    
                    
                        Now let's see how to connect the L298 Based DC motor driver to Arduino.
                    
                    
                        For this we have to configure six Arduino terminals as outputs. Those are for EnA,
                In1, In2 and EnB, In3, In4 connectors of the l298 Module. I've selected Arduino
                pins 8,9,10 and 11, 12, 13 as outputs pins to connect L298 module.
                    
                    
                        Please see the below table to understand the connectivity. I hope you can understand
                the connectivity without a schematic diagram :)
                    
                    
                        
                            
                                | L298 Pin Name | L298 Pin Number | Arduino Connector | 
                        
                        
                            
                                | EnA | 6 | 8 | 
                            
                                | In1 | 5 | 11 | 
                            
                                | In2 | 7 | 12 | 
                            
                                | EnB | 11 | 13 | 
                            
                                | In3 | 10 | 9 | 
                            
                                | In4 | 12 | 10 | 
                        
                    
                    
                        Other than the above mentioned connections, Pin number 9 of the L298 module should
                connect to the +5V output of the Arduino module, Pin numbers 1,8 and 15 to ground
                and pin number 4 to +Vs. ((+) node of your 9V or 12V batteries).
                    
                    
                        Now it is time to connect your DC motors to the output terminals of L298 based DC
                motor driver circuit. Please connect Right side motor of your car to pin numbers
                2, 3 and Left side motor to pin members 13, 14 of the motor driver circuit.
                    
                    
                        And also don’t forget to connect your Arduino board to batteries via DC in connector
                :)
                    
                    Logic and the Source Code
                    
                        If you went through this article carefully from the beginning, now you have a basic
                understanding of how this project works. Now let’s move to the logic.
                    
                    
                        When you press the button A of your RC transmitter, the car should move forward.
                Let’s try to understand the logic behind this. It will be helpful to understand
                the source code.
                    
                    
                        When you press the button A of your RC transmitter, corresponding terminal (in my
                module, pin number D2) of your RC receiver becomes digital 1 (+5V) state. Then the
                pin number 2 of the Arduino module also becomes digital 1 state (please see the
                schematic). Then, according to the logic I previously described in chapter 4, right
                side motor should rotate to right direction and Left side motor should rotate to
                left direction to move the car forward. According to the logic of L298 motor driver,
                for this scenario Both EnA and EnB terminal should go to Digital 1 state. In1, In2
                terminals should go to 1 and 0 state and In3, In4 should go to digital 0 and 1 state.
                We have to program output terminal of Arduino (8, 9, 10, 11, 12, 13) according to
                this scenario
                    
                    
                        Let’s try to understand this logic using a table.
                    
                    
                        
                            
                                | RC Button | Arduino Input Status | Arduino Output Status | Motors Status | 
                        
                        
                            
                                | A (Move Forward) | 2 = HIGH | 8 = EnA = HIGH 9 = In1 = HIGH
 10 = In2 = LOW
 11 = EnB = HIGH
 12 = In3 = LOW
 13 = In4 = HIGH
 
 | Right Motor - Rotating Right Left Motor - Rotating Left
 | 
                            
                                | B (Turn Right) | 3 = HIGH | 8 = EnA = HIGH 9 = In1 = LOW
 10 = In2 = LOW
 11 = EnB = HIGH
 12 = In3 = LOW
 13 = In4 = HIGH
 
 | Right Motor - Turn Off Left Motor - Rotating Left
 | 
                            
                                | C (Turn Left) | 4 = HIGH | 8 = EnA = HIGH 9 = In1 = HIGH
 10 = In2 = LOW
 11 = EnB = HIGH
 12 = In3 = LOW
 13 = In4 = LOW
 
 | Right Motor - Rotating Right Left Motor - Turn Off
 | 
                            
                                | D (Reverse) | 5 = HIGH | 8 = EnA = HIGH 9 = In1 = LOW
 10 = In2 = HIGH
 11 = EnB = HIGH
 12 = In3 = HIGH
 13 = In4 = LOW
 
 | Right Motor - Rotating Left Left Motor - Rotating Right
 | 
                        
                    
                    
                        I hope now you can understand the simple logic behind our RC car. Now it is time
                to convert the above mentioned logic into simple Arduino code.
                    
                    
/*
 Arduino based RC robo car
 
Coded by : Dinesh Kumar Wickramasinghe
 : Sri Lanka
Projects Home : http://www.manelsoft.com/projects/projects_home.aspx
 Project Page : http://www.manelsoft.com/projects/arduino_rc_car.aspx
*/
//RC Receiver input pins
const int A  =   2;
const int B  =   3;
const int C  =   4;
const int D  =   5;
//Output pins to L298 DC Motor Driver
const int EnA     =   8; 
const int InOne   =   9;
const int InTwo   =  10;
const int EnB     =  11;
const int InThree =  12;
const int InFour  =  13;
int Astate     =   0;
int Bstate     =   0;
int Cstate     =   0;
int Dstate     =   0;
//Configure input, output pins
void setup() {
  
  pinMode(A, INPUT);
  pinMode(B, INPUT);
  pinMode(C, INPUT);
  pinMode(D, INPUT);
  pinMode(EnA, OUTPUT);
  pinMode(EnB, OUTPUT); 
  pinMode(InOne, OUTPUT); 
  pinMode(InTwo, OUTPUT); 
  pinMode(InThree, OUTPUT); 
  pinMode(InFour, OUTPUT);       
}
void loop(){
  
  //Read the status of RC Receiver module
  int Astate     =   digitalRead(A);
  int Bstate     =   digitalRead(B);
  int Cstate     =   digitalRead(C);
  int Dstate     =   digitalRead(D);
  
  //If button A pressed, call the move forward function
  if (Astate == HIGH) {       
      MoveForward();
  } 
  //If button B is pressed, call the turn right function
  else if(Bstate == HIGH){
      TurnRight();
  }
  //If button C pressed, call the turn left function
  else if(Cstate == HIGH){
      TurnLeft();
  }
  //If button A pressed, call the reverse function
  else if(Dstate == HIGH){
      Reverse();
  }
  //Else stop the car
  else
  {
      digitalWrite(EnA, LOW); 
      digitalWrite(InThree, LOW); 
      digitalWrite(InFour, LOW); 
      
      digitalWrite(InOne, LOW); 
      digitalWrite(InTwo, LOW); 
      digitalWrite(EnB, LOW); 
  }
}
//Move forward function
void MoveForward()
{
      digitalWrite(EnA, HIGH); 
      digitalWrite(InOne, HIGH); 
      digitalWrite(InTwo, LOW); 
      
      digitalWrite(EnB, HIGH); 
      digitalWrite(InThree, LOW); 
      digitalWrite(InFour, HIGH);   
}
//Turn right function
void TurnRight()
{
      digitalWrite(EnA, HIGH); 
      digitalWrite(InOne, LOW); 
      digitalWrite(InTwo, LOW); 
      
      digitalWrite(EnB, HIGH); 
      digitalWrite(InThree, LOW); 
      digitalWrite(InFour, HIGH); 
}
//Turn left function
void TurnLeft()
{
      digitalWrite(EnA, HIGH); 
      digitalWrite(InOne, HIGH); 
      digitalWrite(InTwo, LOW); 
      
      digitalWrite(EnB, HIGH); 
      digitalWrite(InThree, LOW); 
      digitalWrite(InFour, LOW); 
}
//Reverse function
void Reverse()
{
      digitalWrite(EnA, HIGH); 
      digitalWrite(InOne, LOW); 
      digitalWrite(InTwo, HIGH); 
      
      digitalWrite(EnB, HIGH); 
      digitalWrite(InThree, HIGH); 
      digitalWrite(InFour, LOW);  
}
                    
                    
                        Hope you can understand the code. If you are new to Arduino, please refer any introduction
                online sources.
                    
                    
                        This is the end of our project; below you can see the video of final version of
                RC car I have uploaded to YouTube.
                    
                    
                    Conclusion
                    
                        If you met with any problem, please check your connections again. Try changing the
                power lines of DC motors because the rotational direction of the DC motors are based
                on the direction of the power connectivity. I didn’t explain how I create the chassis
                of this car. I used some plastic boards from an unused printer and sum nuts and
                bolts to build my chassis. Please see the images there at the beginning of this
                web page. If you are difficult to build the chassis, you can buy a readymade chassis
                with motors and wheels like below image from the market or from EBay.
                    
                    
                    
                        Hope you enjoyed the article. If you fare facing any difficulty creating this project
                or if you found any mistake made by me in this article, please comment or contact
                me. Have a nice day!
                    
                    
                        Author - Dinesh Kumar Wickramasinghe (Last Update : 2017-April-12)