Main Menu

Secondary Menu

Raspberry PI Remote Controlled Car

Introduction: Raspberry PI Remote Controlled Car

I am from the days when first personal computer was born with Zilog Z-80 microprocessor. Those days we have to write program codes in Assembly language. Write program into EEPROMs via eeprom burners and edit/erase program in EEPROM via UV Erasers. Basic I/O devices I used old telephone key pads, output device six 7-segment displays. It was a painstaking process to make a project out of microprocessors. Still we enjoyed it.

Later when I engaged in employement lost touch with electronics. Last year I came across Raspberry PI with all the input out put facilities.Then any ICs, accessories, DIY kits available for online purchasing at very low prices. I realised how much lucky this generation of electronic hobbyists.

Coming back to this project, I use the Raspberry PI to make a remote controlled car. If you can have the parts I used or equivalent parts this Raspberry PI controlled can be easily built.

This software code is very basic. This code will to use IR Remote controller as the controlling device. Once you understand the code you can modify it to add sophistication.

Step 1: The Parts Used

1. Raspberry PI

2. DIY Kit 2WD Smart Robot Car

3. L298N Dual H Bridge DC stepper motor drive controller

4. HX 1838 Infrared IR Wireles Remote Control Sensor

5. Samsung TV Remote (or any other)

6. Battery bank. Use a phone charging unit which has 2 outputs.

Item 2, 3, 4 I purchased from ebay. You can purchase theseitems freely on any online shopping sites. Spend a little bit of time to find the lowers cost ones.

Step 2: Making Infrared Sensor and IR Controller Working With Raspberry PI.

In one of my earlier project I managed to write a simple Python code to detect IR signals without using LIRC. I used the HX 1838 IR sensor which I bought online.

The sensor have only 3 pins.

a. 3.3V power pin

b. GND Pin

c. IR sensor pin.

The sensor I purchased comes with simple remote controller. But I used a standard Samsung Remote controller instead of that. If you have a Samsung TV you can use it with same python codes I wrote. If you use a different remote controller you have to modify program to decode that particular remote. Please use following link to understand more about making IR remote controller working in Raspberry PI. If you have the same Samsung remote I am using then it should work with the python code I used.

control. It is a simple one. You don’t need all the keys for this car control. I only used few key as ‘up’, ‘down’, ‘left’, ‘right’. I also used ‘power’ button to send a shut down message to Raspberry PI so that you can turn off the raspberry PI by clicking the ‘power’ button.

def getKey_IR():

keyval=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

ser = serial.Serial ("/dev/ttyAMA0") # GPIO 14 [TX] GPIO 15 [RX]

ser.baudrate = 2400

for i in range(0,24): # for this remote 2x12 bytes received for 2400 baud

data = ser.read (1) # 12 bytes repeated

keyval[i]=ord (data) # data is read as characters so converted to ASCII and stored in an array

Step 3: Make Your DC Stepper Motor Controller Working

LN298 stepper motor controller is a convenient module to control the motors to drive the car.

Introduction to stepper motor control.

It has 2 motor controllers. It allows you drive the motor forward and backward by control signals allowing you to drive the car front or reverse. It also allows to control speed using PWM (pulse width modulation) control.

PWM control allows to vary the power by changing the pulse width of the signal. This motor controler also be used without pulse width control. In that mode you can have only one speed. You cannot increase ot decrease the speed. Pulse width use the term duty cycle. If the duty cycle is 90%-100% you give full power to motor thus to car. If duty cycle less like 10% less power.

I used PWM. Once you know how to use it is a good tool to experiment. Once you go through this project you should be able to figure out how to get it working.

How to use the stepper motor controller

There are 14 pins in this unit which give you full control of manipulating the motors.

Pin 1,2 and Pin 13,14 would control the 2 motors. If the output vaoltage is positive motor rotates in one direction. If negative in opposite direction. You connect these connections to the 2 motors of your car.

The motors can be driven by 5V or higher voltages. Since the car motors can be run at 5V, I used 5V. So the same battery (my phone charger battery) can provide 5V for this motor controller and as well as for the Raspberry PI. Keep the shunt No 3 as it is since we use 5Vs.

Pin 4 & 5 are 5V & GND connections from the battery. Leave Pin 6 unconnected. That is for a different purpose.

Pin 8, 9 and Pin 10, 11 are used to determine direction of rotation of motor. For example if Pin 8 high (5V) and Pin 9 is low (0V) the motor rotates in one direction. if Pin 8 low (0V) and Pin 9 is high (5V) the motor rotates in opposite direction. Same for Pin 10, 11.

We get 4 GPIO pins to control these 4 pins. You can controldirection of the car travel by manipulating these PINs. Further if you make only one motor to rotate while keeping other stationary you can get the car to turn left or right. That’s how you turn your car.

The remaing 2 pins are Pin 7 and Pin 12. These 2 pins are input PWM signal from Raspberry Pi to speed control the 2 motors.

For more information read LN298 data sheet and specifications to further understand how it works in detail. You can google it out.

How to manage PWM in Raspberry PI.

There are several libraries to use PWM in Raspberry PI. After trying several finally I found using GPIO library is more convenient. Initially I could not find how to use GPIO library for PWM, later I found materials in internet about it. This is how you use it.

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11,GPIO.OUT) # motor1 pwm

GPIO.setup(13,GPIO.OUT) # motor2 pwm

# motor1

my_pwm=GPIO.PWM(11,160) # set frequency GPIO.PWM(pin,frequency)

my_pwm.start(50) # start duty cycle 50%

my_pwm.ChangeDutyCycle(75) # change duty cycle to 75%

# motor2

pwm2=GPIO.PWM(13,160) # set frequency GPIO.PWM(pin,frequency)

pwm2.start(50) # start duty cycle 50%

pwm2.ChangeDutyCycle(75) # change duty cycle to 75%

Now by changing dutycycle you can increase or decrease your car speed. As simple as that.

Step 4: The Battery

For convenient I used my phone charger battery. If the

battery has 2 out put it would be good since you need one power for Raspberry PI and another to drive the motor controller. You can use phone USB power cable to connect Raspberry PI. Use another old USB cable to connect the battery to motoro controller.

Cut the smaller connector and expose the cables. Some cable has 2 pairs, some 1 pair of wire. If one pair it is easier because that pair would be 5V. Use volt meter to find out which gives the 5V output and which wire is positive and which one is negative. Take care not to short this 5V cable pair, it may cuase short circuit. Connect the loose end to Motor controller before connecting the battery. Make sure 5V and GND cables are connected to correct pins Pin 4 and Pin 5.

The motor driver need more power. If your battery not charged properly or low ampere rating you may find the motors not moving. When I am testing the motor controller using 5V power sources of phone chargers found most normal phone chargers don’t have enough power to make the motor to run. Later I found a bigger 5V power adoptor used for my old ADSL modem (5V 1.2Amp) rating to power the motor & motor control for testing.

Step 5: Sequence to Get Started

First get your assemble work on breadboard.

Refer the attached circuit diagram that I prepared. This has all the pin connection details Raspberry PI, IR Sensor, LN298 motor controller.

Start with IR remote controller working first. Check if you can press remote keys and the pyhton code in Raspberry PI.

Once it start working start testing your motor controller. Assemble you DIY motor car. I used a car with 2 motors. There 4 motor cars as well in online markets. If you use it plan for a better battery power sine it may need more power.

Once the car is assembled check if the motor and wheels moving by giving 5Vs to motor.

Next connect the LN298 motor controller to Raspberry PI.

For testing and trouble shooting of PWM out puts you could have simple LED with Resistor connected parallely to the PWM outputs. If you are interested further analysing PWM and other GPIO outputs you can try using USB Logic Analysers. There are low cost USB Logic Analyzer Devices (24Mhz) you can buy online. I used one and installed software in my laptop. Software also you can download from internet. It is a very handy debugging tool to have.

Python codes;

Drive forward

def movefront():

# motor1

my_pwm=GPIO.PWM(11,160) # set frequency GPIO.PWM(pin,frequency)

my_pwm.start(50)

my_pwm.ChangeDutyCycle(75)

GPIO.output(12,0)

GPIO.output(16,1)

# motor2

pwm2=GPIO.PWM(13,160) # set frequency GPIO.PWM(pin,frequency)

pwm2.start(50)

pwm2.ChangeDutyCycle(75)

GPIO.output(18,0)

GPIO.output(22,1)

time.sleep(2) # let it move for 2 seconds

pwm2.stop()

Turn Left

def leftturn():

# motor1

my_pwm=GPIO.PWM(11,160) # set frequency GPIO.PWM(pin,frequency)

my_pwm.start(50)

my_pwm.ChangeDutyCycle(75)

GPIO.output(12,0)

GPIO.output(16,1)

time.sleep(0.5) # let it turn for 0.5 seconds

my_pwm.stop()

Once it works in breadboard move Raspberry PI, IR sensor, LN298 motor controller, battery on to car and fix it.