Friday 15 January 2016

Android App controlled 4WD RC Car Kit Assembly Tutorial










1. The Kit

This kit is available from our eBay store.
It comes with the following  parts:

4 x Geared motors + 4 x Tyres/wheels + 4 x Motor fixings
4 x black + 4 x red motor cables.
2 x  transparent acrylic glass plate 250 x 160 x 3mm
1 x L298N motor driver
1 x  Arduino UNO328 controller board (CH340 usb driver installation needed.)
1 x HC-06 Bluetooth module
1 x Mini breadboard
2 x Battery holder (6V , 4 x AA / batteries not included)
1 x Battery holder (9V ,  batteries not included)
4 x motor speed encoding disks (Use with IR optocoupler sensors which are not included)
30 x 20 cm Dupont jumpers cable, 10 each(M-M,M-F,F-F)
1 x USB cable for arduino 
6 x Copper pillar ( 30 mm ),  1 x Screws kit 
Some Extras: on/off toggle mini switch, extra screws and nuts, dc jack for Arduino battery, tie cables etc


2. Connect the motors


Connect the motors and wheels as shown in the  figure 1.  Solder the red and black wires as shown. These correspond to the positive and negative of the motors/battery terminals. If you connect the red and black to the +ve and -ve of the battery , all the motors/ wheels should spin in the same direction. If any wheel spins in a different direction swap the wires on the motor terminals to correct the spin.

Figure 1: Connecting wheels and motors




2. Connect L298n motor board
Connect the red and black wires from two right side motors to +/-ve terminal on L298n  board which are numbered 1/2 respectively.

Figure 2: L298N motor driver pin outs

Similarly Connect the red and black wires from two left side motors to +/-ve terminal on L298n  board which are numbered 13/14 respectively.



Figure 3 Connecting red and black wires to L298n driver.



4. Connect L298n motor control pins to Arduino
Attach top plate and screw Arduino to it. Next, connect your power supply - the positive to pin 4 on the module and negative/GND to pin 5. If you supply is up to 12V you can leave in the 12V jumper (point 3 in the image above) and 5V will be available from pin 6 on the module. This can be fed to your Arduino's  5V pin to power it from the motors' power supply. Don't forget to connect Arduino GND to pin 5 on the module as well to complete the circuit. 
Now you will need six digital output pins on your Arduino, two of which need to be PWM (pulse-width modulation) pins. PWM pins are denoted by the tilde ("~") next to the pin number, as shown in Fig 4:

Figure 4 Arduino Pinouts

Finally, connect the Arduino digital output pins to the driver module. In our example we have two DC motors, so digital pins D9, D8, D7 and D6 will be connected to pins IN1, IN2, IN3 and IN4 respectively. Then connect D10 to module pin 7 (remove the jumper first) and D5 to module pin 12 (again, remove the jumper). 


L298n
Arduino
7
D10
IN1(8)
D9
IN2(9)
D8
IN3(10)
D7
IN4(11)
D6
12
D5

Table 1 Arduino connections to L298N

The motor direction is controlled by sending a HIGH or LOW signal to the drive for each motor (or channel). For example for motor one, a HIGH to IN1 and a LOW to IN2 will cause it to turn in one direction, and  a LOW and HIGH will cause it to turn in the other direction.
However the motors will not turn until a HIGH is set to the enable pin (7 for motor one, 12 for motor two). And they can be turned off with a LOW to the same pin(s). However if you need to control the speed of the motors, the PWM signal from the digital pin connected to the enable pin can take care of it. The range of speed possible of your hardware, set the PWM values between zero to 255.

Figure 5 Connect to Arduino


5. Connect bluetooth hc-06 module to Arduino


Table 2 and Fig show how you should connect the Bluetooth module to your Arduino.

HC-06
Arduino
VCC
3.3v
GND
GND
TXD
RXD
RXD
TXD

Table 2 Arduino connections to Hc-06

Figure 6: hc-06 connections to Arduino

You can also connect the VCC to 5v though that is not advised, especially if you are using the setup for long.
The HC-06 acts as a serial port through which you can send and receive data. So using a serial terminal or a Bluetooth customized application on your computer or phone, you can control and monitor your project. you can use Teraterm  for windows or Bluetooth Termial for android device as the serial terminal.
Before, uploading the code to the Arduino, disconnect the HC-06 module, since it shares the tx/rx pins and will interfere with the upload. Connect it back once the code has been uploaded successfully.

The full setup is shown Fig 7 .  I have used 6v (4xAA) to power Arduino and separate 12 V (8xAA) to power the motors board.



Figure 7: hc-06 connection to arduino




6. Arduino code:

// This code is for the bluetooth  controlled  arduino car.
// Bluetooth is always enabled and available . The password is 1234.
// Android car control app can be found here:
// https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller&hl=en
//Pin assignments and global variables per function. Customize if needed
//*******Pin assignments Motor board ********************

const int MotorRightPWM = 10;
const int MotorRight1 = 9;
const int MotorRight2 = 8;

const int MotorLeftPWM = 5;
const int MotorLeft1 = 7;
const int MotorLeft2 = 6;
int iSpeed = 255; //speed, range 0 to 255
const int LedPin=13;
//*****Bluetooth signals**************************************************
char val; //stores received character. Needs to be global to perform continuous movement

//*********General SETUP: activate pins***********************************
void setup() {
 //start receiving serial infor
 Serial.begin(9600);
 //motor connections
 pinMode(MotorRight1, OUTPUT);  //
 pinMode(MotorRight2, OUTPUT);  //
 pinMode(MotorLeft1,  OUTPUT);  //
 pinMode(MotorLeft2,  OUTPUT);  //
 pinMode(MotorRightPWM, OUTPUT); //enable for right side motor
 pinMode(MotorLeftPWM, OUTPUT); //enable for right side motor
}
//**************Movement functions******************************
void advance(int d) { //go straight
 digitalWrite(MotorRight1, HIGH);
 digitalWrite(MotorRight2, LOW);
 digitalWrite(MotorLeft1, HIGH);
 digitalWrite(MotorLeft2, LOW);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);
 delay(d * 10);
}
void right(int d) { //turn right (single wheel)
 digitalWrite(MotorLeft1, LOW);
 digitalWrite(MotorLeft2, HIGH);
 digitalWrite(MotorRight1, LOW);
 digitalWrite(MotorRight2, LOW);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);
 delay(d * 10);
}
void left(int d) {//turn left(single wheel)
 digitalWrite(MotorRight1, LOW);
 digitalWrite(MotorRight2, HIGH);
 digitalWrite(MotorLeft1, LOW);
 digitalWrite(MotorLeft2, LOW);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);
 delay(d * 10);
}
void turnR(int d) {//turn right (two wheels)
 digitalWrite(MotorRight1, HIGH);
 digitalWrite(MotorRight2, LOW);
 digitalWrite(MotorLeft1, LOW);
 digitalWrite(MotorLeft2, HIGH);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);  
 delay(d * 10);
}
void turnL(int d) {//turn left (two wheels)
 digitalWrite(MotorRight1, LOW);
 digitalWrite(MotorRight2, HIGH);
 digitalWrite(MotorLeft1, HIGH);
 digitalWrite(MotorLeft2, LOW);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);
 delay(d * 10);
}
void stopp(int d) { //stop
 digitalWrite(MotorRight1, LOW);
 digitalWrite(MotorRight2, LOW);
 digitalWrite(MotorLeft1, LOW);
 digitalWrite(MotorLeft2, LOW);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);  
 delay(d * 10);
}
void back(int d) { //go back
 digitalWrite(MotorRight1, LOW);
 digitalWrite(MotorRight2, HIGH);
 digitalWrite(MotorLeft1, LOW);
 digitalWrite(MotorLeft2, HIGH);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);  
 delay(d * 10);
}


//*************************Bluetooth functionality***********************
//Bluetooth commands
void bluetoothCommand() {
 if (Serial.available()) { //check if bluetooth command available
   val = Serial.read();
   Serial.write(val);
 }
 if (val == 'F') { // Forward
   advance(10);
 }
 else if (val == 'S') { // Stop Forward
   stopp(10) ;
   val = Serial.read(); //read value again, otherwise can't continu with infrared
 }
 else if (val == 'B') { // Backwards
   back(10);
 }
 else if (val == 'R') { // Right
   turnL(10);
 }
 else if (val == 'L') { // Left
   turnR(10);
 }
 else if (val == 's') { // Stop, not used though
   stopp(10 ) ;
 }
 else if (int(val) >= 49 && int(val) <= 57) { //set speed
   iSpeed = (int(val)-48)*26;
   Serial.println("Speed set to: " + iSpeed);
 }
 else if (val == 'q') { //set speed
   iSpeed = 255;
   digitalWrite(LedPin,HIGH);   
   Serial.println("Speed set to: " + iSpeed);  
 }
 else if (val == 'W') {
   digitalWrite(LedPin,HIGH);
 }
 else if (val == 'w') {
   digitalWrite(LedPin,LOW);
 }
}
//**************************************MAIN LOOP***************************************
void loop() {
 //bluetooth commands
 bluetoothCommand();
}

7. Android App

The link to Download the app from google play store is:
A screen shot of the app is shown in Fig 8. The app has both accelerometer and button control functionality.
Figure 8: Bluetooth RC Controller











1 comment: