Controlling a Raspberry Pi RC Car With a Keyboard




In this intermediate tutorial you will learn how to operate a hacked RC car with a keyboard using a model B Raspberry Pi device using Python. The key points in this tutorial include:
Controlling a Raspberry Pi RC Car With a Keyboard
• Configuring the virtual Pulse Width Modulation (PWM) for the GPIO pins so two DC motors can run independently
• Wiring the Raspberry Pi to the RC car

I had originally planned to have the car operated through the use of an IR remote control and receiver but due to compatibility issues between the Raspberry Pi and the required libraries I had to revise my project.

Step 1: Components

Please ensure you have the following components before continuing with the tutorial:

• Raspberry Pi Model B (http://www.adafruit.com/products/998)
• MicroSD Card (http://www.adafruit.com/products/102)
• Pi Cobbler Breakout and Cable (http://www.adafruit.com/products/914)
• Any sized Breadboard (http://www.adafruit.com/products/239)
• M/M Wires (http://www.adafruit.com/products/153)
• F/F Wires (http://www.adafruit.com/products/266)
• Prototyping Pi Plate (http://www.adafruit.com/products/801)
• L293D Chip (http://www.adafruit.com/products/807)
• Medium sized RC Car with DC Motors
• Bluetooth Keyboard
• Soldering Iron and Wire

Step 2: Prerequisites

Please ensure you meet the following prerequisites before continuing with the tutorial:• An assembled Cobbler with GPIO Cable and Breadboard• Soldering iron experience• An upgraded operating system. You can achieve this by entering into the terminal:

sudo apt-get upgrade

• Up-to-date GPIO library. You can achieve this by entering into the terminal:
sudo apt-get update
sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio

Step 3: The L293D Chip

Controlling a Raspberry Pi RC Car With a Keyboard Diagram

The most important part of your hardware setup is the L293D chip. With the appropriate code, this chip allows you to control the speed and direction of two independent DC motors. It is crucial for you to understand how this chip works and the function of each of its pins. The ‘+Vmotor’ pin (8) provides the power for the motors while the ‘+V’ pin (16) provides the power for the chip’s logic. The ‘IN’ pins (2, 7, 10, 15) each require a connection to a GPIO pin and the ‘OUT’ pins (3, 6, 11, 14) provide the output for the two DC motors.

Step 4: Hardware

Once you understand the mechanics of the L293D chip it is time to assemble it, along with the rest of the components, onto the breadboard. The schematic has been provided for you. Please note the orientation of the L293D chip.

You may notice the LEDs on the diagram. These represent the headlights or neon underside lights on your RC car. If your car lacks these features please disregard this section of the schematic.

Step 5: Software

Below is the python code you will need in order for this program to work. Save the program as rc_car.py when finished.

import RPi.GPIO as io
io.setmode(io.BCM)
import sys, tty, termios, time

# These two blocks of code configure the PWM settings for
# the two DC motors on the RC car. It defines the two GPIO
# pins used for the input, starts the PWM and sets the
# motors’ speed to 0
motor1_in1_pin = 4
motor1_in2_pin = 17
io.setup(motor1_in1_pin, io.OUT)
io.setup(motor1_in2_pin, io.OUT)
motor1 = io.PWM(4,100)
motor1.start(0)
motor1.ChangeDutyCycle(0)

motor2_in1_pin = 24
motor2_in2_pin = 25
io.setup(motor2_in1_pin, io.OUT)
io.setup(motor2_in2_pin, io.OUT)
motor2 = io.PWM(4,100)
motor2.start(0)
motor2.ChangeDutyCycle(0)

# Defining the GPIO pins that will be used for the LEDs on
# the RC car and setting the output to false
io.setup(18, io.OUT)
io.output(18, False)

io.setup(23, io.OUT)
io.output(23, False)

# The getch method can determine which key has been pressed
# by the user on the keyboard by accessing the system files
# It will then return the pressed key as a variable
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch

 

For more detail: Controlling a Raspberry Pi RC Car With a Keyboard




© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top