Motion Controlled IR RC Car
So a couple of weeks or months ago, I saw this concept CGI video on Instagram of a car that was being controlled with help of this remote which was completely motion-sensitive. This gave me an idea, what if we bring that concept to life with an Arduino. So I spent a little time thinking about how I'd tackle this and came up with a solution which ended up being pretty fun. Now the car I made uses the super slow gear-motors and also the remote I made is completely on a breadboard, and I hope in the future, as I learn more about designing circuits, I'll be able to create a much more compact version of the remote and a much faster car. And probably 3D Print a Tesla Roadster Chasis to make it look like I made a motion-controlled Tesla. Well tbh I am new to Arduinos and Electronic Engineering as well, so even if I am good at coding, I cannot really make something that looks more legit.
Anyway, if you're making this project as well then let me tell you what the final product can do,
So firstly, I unfortunately did not have the radio transceiver modules, the NRFs, so I had to use something to make it wireless, and guess what I used? My favorites, the Infrared LED and Receiver. The only problem with using IR for communication is, IR is mono-directional so technically the car would not have a very good range for controlling and I myself would suggest you to rather use radio transceivers instead.
Secondly, It is motion-sensitive, technically it runs based on the accelerometer data, this project has got two parts so you're gonna need two Arduinos, I personally used an Uno and a Nano but you can use any of the two. Also since it uses IR receivers, you can also control this car with the help of your TV remote, like I showed in the video above.
Collect Components
So starting off with the components, there's a couple of special components that we are going to use in this project that you might not have used in a project before. Therefore, read this article carefully and in case if you get stuck, you can always reach out to me for help.
Also like I said before, you need two Arduinos for this project (Doesn't matter which Arduino it is, you just need two of them).
For the car:
- Arduino Uno
- L298N 2A Dual Motor Driver
- An IR Receiver
- 2xGear Motors with wheels
- A Powerbank with 5V output at 1A (You can use a 3xAA battery pack just to get the high current).
For the remote:
- Arduino Nano
- MPU 6050 Accelerometer and Gyroscope sensor
- An IR LED
- A 9V battery
And also you need a bunch of jumper wires and a couple of breadboards if you are making it like I did, if you want you can also print your own PCB and 3D print the car and basically make a much better version of this project, I'd love to see it!
Circuit Designs
And here are the circuit designs for the remote and the car. You can take a close look at the images in order to have a clear understanding of how the circuits are designed. Now remember, if you have any problems understanding what exactly and how exactly the circuit is working, feel free to reach out to me.
I have attached the Fritzing files in order for you to play with the circuit design on your computer, you can download it and check it out.
Building the Circuit and the Structure
Here are the step-by-step instructions to build your circuit just in case if the images and the design files were not enough.
Starting off with the Car,
- Take your battery pack and connect it to the +/- rails of the breadboard accordingly.
- Now with the help of a jumper wire, simply connect the positive to the 12V of the L298N Motor Driver and connect the ground to the ground of the Motor Driver. So you're basically connecting the positive and ground of the battery pack to the 12V and GND of the Motor Driver.
- Now connect the GND of the Arduino UNO to the GND rail of the breadboard hence connecting the ground of Arduino to the ground of the battery pack.
- Next, connect the Vin pin of Arduino to the 5V output of the Motor Driver.
- After that, you can connect your gear-motors to the M1, M2, M3, and M4 terminals of the motor driver accordingly, you may need to switch the terminals later in case if the motor turns the wrong way.
- Connect Digital Pins 12, 11, 10, 9 of Arduino to IN1, IN2, IN3, IN4 of Motor Driver respectively with the help of jumper wires. Now your motor driver, Arduino, and motors are all connected.
- Now connect your IR Receiver to your Arduino by simply connecting the VCC of the Receiver to the Arduino's 5V output.
- Then connect the GND of the IR Receiver to the GND of Arduino.
- And finally, connect the output pin of the Receiver to Digital Pin 2 of Arduino.
So now your car's circuit is ready, you can use the breadboard holes as you like just make sure that all the connections are made to the corresponding pins correctly. There are multiple components so make sure the connections are right when using a breadboard because you might make the wrong connections by mistake.
Now the remote,
- Connect your 9V battery's Positive and Ground Terminals to the +/- rails of the breadboard accordingly.
- Now connect the Positive and Ground to the Vin and GND of the Arduino Nano respectively.
- Next, you can connect the Arduino's Pin D2 to the IR Emitter/LED's anode. You can put a resistor but since the IR LED operates at a higher voltage, it won't hurt it without a resistor.
- Now connect the cathode of the IR LED to the GND of the Arduino.
- Next make connections of the MPU6050 with the Arduino:
- Connect the VCC of the MPU6050 to the 5V output of the Arduino.
- Connect the GND of the MPU6050 to the GND of the Arduino.
- Connect the SCL pin of the MPU6050 to Analog Pin A5 of the Arduino.
- Connect the SDA pin of the MPU6050 to Analog Pin A4 of the Arduino.
Now the connection of your remote is done so you can now move ahead with the project. All you're left to do with the structural design is to make the car's structure. I made my remote all on top of the large size breadboard, and I used a hardboard in order to make the base for the car. I simply glued on the gear motors and put a small rod in place of the front wheels that would get dragged along when the car operates, you can see it in the video. I wanted to have a better structure but to be honest, I started working on this project as an experiment and didn't think it would work. Especially with IR Transceivers, and when it did work, I kinda blew my own mind a little. However, though you can build a better model of the car and I'd love to see it!
Coding
Here's the code you need in order to bring life into your car and remote. If you've made all the connections right then the code should work just right.
Here's a list of packages that we'd be using that you can either get from the Arduino Library or it is already available by default. Make sure you have the right versions installed.
- IRremote by shirriff, z3t0, ArminJo - v2.5.0
- MPU6050 by Electronic Cats - v0.2.1
- I2Cdev and Wire - These packages are available by default, you don't need to install it separately.
Code for the Car,
#include <IRremote.h> IRrecv irrecv(2); decode_results results; #define motorLeft1 12 #define motorLeft2 11 #define motorRight1 10 #define motorRight2 9 void halt() { Serial.println("Halting..."); digitalWrite(motorLeft1, LOW); digitalWrite(motorLeft2, LOW); digitalWrite(motorRight1, LOW); digitalWrite(motorRight2, LOW); } void forward() { Serial.println("Going backward..."); digitalWrite(motorLeft1, LOW); digitalWrite(motorLeft2, HIGH); digitalWrite(motorRight1, LOW); digitalWrite(motorRight2, HIGH); } void backward() { Serial.println("Going forward..."); digitalWrite(motorLeft1, HIGH); digitalWrite(motorLeft2, LOW); digitalWrite(motorRight1, HIGH); digitalWrite(motorRight2, LOW); } void turnLeft() { Serial.println("Turning left..."); digitalWrite(motorLeft1, LOW); digitalWrite(motorLeft2, HIGH); digitalWrite(motorRight1, HIGH); digitalWrite(motorRight2, LOW); } void turnRight() { Serial.println("Turning right..."); digitalWrite(motorLeft1, HIGH); digitalWrite(motorLeft2, LOW); digitalWrite(motorRight1, LOW); digitalWrite(motorRight2, HIGH); } void setup() { pinMode(motorLeft1, OUTPUT); pinMode(motorLeft2, OUTPUT); pinMode(motorRight1, OUTPUT); pinMode(motorRight2, OUTPUT); Serial.begin(74880); irrecv.enableIRIn(); halt(); } void loop() { if (irrecv.decode(&results)) { int sign = results.value; switch(sign){ case 241307256: forward(); break; case 2873260659: backward(); break; case 951854522: turnLeft(); break; case 4282632653: turnRight(); break; // Remote Codes present down below, replace them with the remote codes received from your remote's buttons case 752: forward(); break; case 2800: backward(); break; case 720: turnLeft(); break; case 3280 : turnRight(); break; default: halt(); Serial.println(sign); } irrecv.resume(); } }<br>
Code for the remote,
#include <IRremote.h> #include "I2Cdev.h" #include "MPU6050.h" #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE #include "Wire.h" #endif MPU6050 accelgyro; int16_t x, y, z; int16_t gx, gy, gz; #define OUTPUT_READABLE_ACCELGYRO IRsend IrSender; #define goForward 22 #define goBackward 11 #define goRight 21 #define goLeft 12 #define halt 33 void setup(){ #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE Wire.begin(); #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE Fastwire::setup(400, true); #endif Serial.begin(74880); Serial.println("Initializing I2C devices..."); accelgyro.initialize(); Serial.println("Testing device connections..."); Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); } void loop(){ accelgyro.getMotion6(&x, &y, &z, &gx, &gy, &gz); Serial.print("AngleX= "); Serial.println(x); Serial.print("AngleY= "); Serial.println(y); Serial.print("AngleZ= "); Serial.println(z); Serial.println("-----------------------------------------"); if(y > 4000) { // Go Left Serial.println("Go Left"); IrSender.sendNEC(goLeft, 12); } else if(y < -8000) { // Go Right Serial.println("Go Right"); IrSender.sendNEC(goRight, 12); } else if(x < -3500){ // Go Front Serial.println("Go Front"); IrSender.sendNEC(goForward, 12); } else if( x > 5000){ // Go Back Serial.println("Go Back"); IrSender.sendNEC(goBackward, 12); } else{ Serial.println("Halt"); IrSender.sendNEC(halt, 12); } delay(100); }<br>
Now you can simply upload this code and you're literally ready to go, although in order to control the car with your TV remote, you need to record your TV Remote Button signals manually and editing it in the code. Only after that you'd be able to control your car with the help of your IR TV remote as well. Now you might be asking how am I supposed to do that, well for that, all you need to do is connect your car's Arduino to your computer and then open the serial monitor. Next you need to simply keep pressing the buttons, and you'll notice there are unique codes for every button you press on your remote, you need to replace these codes with the once that are given in the code sketch and basically after that you'd be able to control your car with the help of your remote.
And Here We Go!
Make sure all your connections to every pin is correct, the code is uploaded, and the remote codes has been replaced with the ones that are emitted by your remote. If you have followed all the previous steps properly, then there shouldn't be any issue and everything should run. Hence your calculator is ready!
It should work just the way I showed in the video above. If it doesn't, feel free to reach out to me on Twitter I'll help you out in case you face any kind of problem.
Now you can either tilt your remote on various directions or simply press the buttons that you have assigned a function to in the switch-case in the code and move your car accordingly. You can test for bugs and in case if you wish to make changes/improvise the code then you can simply create an issue or make a pull request to the code repository. If it works perfectly, then congrats! You have made your own remote control calculator successfully, great job! Hope to see you make something better and improvise this project!
Thank You!