Build a Semi-autonomous, 4WD Raspberry Pi Car with a camera stream!
Story
This is a guide on how to build my Pi Car!
I recall discovering the Raspberry Pi and being intensely curious about electronics and coding. Desperate to create innovative gadgets, I was faced with the daunting prospect of starting from scratch, having no prior experience with coding or electronics.
My first project was a bold endeavor – building a Raspberry Pi Car! As I dove into the project, I simultaneously sought to grasp the fundamental concepts of the Raspberry Pi, Linux, electronics, and Python. It was a thrilling learning experience that allowed me to combine creativity and technical expertise.
I’m happy to share my knowledge with fellow starters! With the goal of empowering beginners, I’ve crafted a comprehensive guide. By weaving together links to various online resources, tutorials, and guides that I utilized during my Pi Car project, my aim is to provide a steady compass for those embarking on their own Raspberry Pi adventures.
Ultimate Goal:
Build a Pi Car that has the following:
-4WD (Done!)
-Semi-Autonomous Capability (Done!)
-Camera Stream (Accessible over the Internet) (Done!)
-Automatically runs program on start up (WORK IN PROGRESS! 🙁 )
The Car (To Date!):
Building the Pi Car:
Take a look at the “Components and Supplies” section for the essential parts you’ll need to assemble this project. Make sure you have all the necessary components before you begin building.
1. Building the Chassis:
Assemble the chassis kit. This is rather straight forward but see the pre and post build pictures below for help.
Important:Hold up! Before fixing the motors in place (outline below), connect a red and black wire to each one. Make sure the red wire attaches to the top and black wire to the bottom of the motor. Use insulation tape to secure those connections snugly. Optional: solder for a permanent link. Don’t forget to remove the brown cover from each plastic sheet at the end!
In my build I moved the battery pack towards the end of the chassis to create more space (Indicated by the arrows in the photo below).
2. Connecting the Motors to L298N Motor Driver:
Next we will attach the 4 motors to the motor driver.
2.1 Connecting the wires:
Hint it up! Review the images below to distinguish between the motors and also get a clear view of the motor driver’s wiring. The diagram below outlines the wiring instructions for each of the 4 motors. If you still need a hand, the supplementary Schematics diagram is attached for extra guidance.
Top Left Motor: Top Right Motor:
Red: Out 2 Red: Out 4
Black: Out 1 Black: Out 3
Bottom Left Motor: Bottom Right Motor:
Red: Out 1 Red: Out 3
Black: Out 2 Black: Out 4
Important: Enable Two-Wire Connection: Plug two wires into each ‘OUT’ hole on the motor driver. Tighten the connections to prevent wire movement. Be patient while doing this, as it may take some time. To prevent damage, handle the wires carefully. For improved cable organization, route the wires through the small holes in the chassis.
2.2 Connecting the battery pack:
To connect the battery pack (already positioned on the chassis) to the motor driver connect the wires as follows:
Connect the Red wire into the VCC hole & the Black wire into the GND hole.
(See picture above for guidance)
All the 4 motors are connected to the motor driver now let me add Raspberry pi and connect it with the motor driver also.
3. Setting up L298N Motor Driver to Raspberry Pi
Before adding our Raspberry Pi we need to prepare it by following the steps:
3.1 Preparing the Pi:
1. Download Raspbian Jessie Operating Systems (OS) – AVAILABLE HERE
2. Format & Install Raspbian to SD Card – HOW TO DO GUIDE HERE
3. Insert the Wireless Internet & Wireless Keyboard USB Adapters into the Pi’s USB slots.
3.2 Connecting to the L298N Motor Driver:
After assembling the components, place the completed module at the forefront of the Pi-Car. To secure it, I used screws from a Pi case I had available. Then, utilize jumper wires to link the Pi’s GPIO pins to the Motor Driver.
Connect the Pins as listed below:
- GPIO Pin 2 to 5V (Use Female-to-Male Jumper Wire)
- GPIO Pin 6 to GND (Use Female-to-Male Jumper Wire)
- GPIO Pin 7 to INI4 (EVA)
- GPIO Pin 11 to INI3 (5V)
- GPIO Pin 13 to INI2 (5V)
- GPIO Pin 15 to INI1 (EVB)
To assist with discovering which pins are assignable with the Pi look at the following picture:
Now that the Pi is positioned let’s attach the Ultrasonic sensor.
4. HC-SR04 Ultrasonic Distance Measuring Sensor Module/Foundation
Screw the sensor module and wires into a breadboard according to the bar below: Make sure that you put the 1ohm resistor into the circuit as this will prevent your Pi from being damaged. (Refer to the other picture below of which resistor to select)
Once completed, position the breadboard at the front of the car, on the bottom layer.
4.1 Wiring the sensor to Pi:
Connect the wires on the breadboard to the Pi and Motor driver as follows:
Echo Cable (Yellow Cable in picture above) to Pin 16 on the Pi (Using Female-to-Male Jumper Wire)
Trigger Cable (Orange Cable in picture above) to Pin 12 on the Pi (Use Female-to-Male Jumper Wire)
Ground Cable (Black Cable in Picture above) to GND on the motor driver (Use Female-to-Male Jumper Wire)
Power Cable (Red Cable in Picture above) to 5V on the motor driver (Use Female-to-Male Jumper Wire)
See the pictures of the Pi and the motor driver if you need help identifying which Pins are which.
Congratulations! You should now have all of the necessary cables plugged into the motor driver, except for the ONE cable. The connections for this will be 1 Cable going to the VCC (From the battery pack), 3 cables to the GND (1 from Battery Pack, Raspberry Pi and Ultrasonic Sensor) and 2 cables going to the 5V (1 from the Raspberry Pi and 1 from the Ultrasonic Sensor).
5. Adding the camera to the Pi:
Watch this fantastic Pi Camera Guide on that will will provide:
- An overview of the camera itself
- How to connect the camera module to the Raspberry Pi
- How to enable the camera module on the Pi Itself
- Basic commands to use the camera (i.e how to take pictures and videos)
On my build I used the ‘Scorpi / Flexible Mount’ cable to help secure the camera in position.
6. Position the power pack:
Place this with reference to the back of the chassis, on the second layer from the bottom. Current charger I decided to use was somewhat flimsy, so I has to use an elastic band to hold it in place momentarily as was being used.
Setting up the Pi Car:
Now the car is built we can now develop the software for the Car.
1. Preparing the Pi:
Ensure the following are connected to the Pi before turning it on:
- Wireless Keyboard USB adapter
- Wireless Internet USB adapter
- HDMI cable to a monitor/TV.
- The camera should also be connected.
2. Turning on the Pi:
To start up the Pi insert the micro-USB cable from the power supply into the Pi. The Pi will take a few minutes to boot the first time around. Once this is done, the GUI will be launched and displayed on the screen after it has been developed.
3. Configuring the Pi:
First thing we need to do is configure the Pi. To do so click:
Menu (Top Left of the Screen)>Preferences>Raspberry Pi Configuration
The following window will open:
To optimize the use of our SD Card’s memory capacity, press the “Expand filesystem” button and then have the chance to modify your password and any other settings.
Next, click the ‘Interface’ tab and enable the camera (Ensure the SHH is enabled too.)
Then, click the ‘Localisation’ tab and set the language and country to your preferences.
Click ‘Ok’ and reboot the Pi.
4. Connecting to the internet:
Once the Pi reboots, click the ‘Network’ icon located at the top right of the screen.
This will take you to your router dashboard, choose your router from the drop down list and then type the routers password.
5. Updating the Raspbian OS:
Now we are connected to the internet we can update our OS to its latest version. To do this open the ‘Terminal’ and enter the following codes separately:
sudo apt-get update
sudo apt-get upgrade
(For the second command enter Y when prompted)
Both may take a few minutes to complete and once complete reboot your Pi.
6. Installing VLC:
Of all the applications available in Raspberry, the one to use to broadcast the camera over the internet is VLC. Oh, this has to be done on both, the Pi and the other computer/smartphone through which you will be viewing the stream from the camera.
On the Pi:
Enter the following code into the terminal:
sudo apt-get install vlc
(Enter Y when prompted)
On the client computer/smartphone:
Click here and download the appropriate version for your OS. (Windows/Mac/Android)
7. Creating the Files:
Now our Pi is up-to-date and we have all the required applications can create the code that will actually drive the car.
First, we will create a folder to house our files. To do this we enter the following command into the terminal:
mkdir PiCar
This will create a folder called ‘PiCar’ – feel free to name this whatever you like.
Now the folder has been made we need to move into it by typing the following the command;
cd PiCar
Now we need to create the python files that will actually drive the car:
7.1 main.py – this will be the main file we run to drive the car
To create an empty file type the following command into the terminal:
sudo nano main.py
This will open up a new file within the Raspberry Pi’s text editor. (Called Nano)
Now either copy or type the code, available in the attached main.py file,into this empty file.
7.2 sensor.py – this file will initialise the Ultrasonic sensor.
Complete this process above again for the sensor.py file. Remember to create the file first using the following command;
sudo nano sensor.py
(Ensure you create this whilst within the PiCar folder we created)
7.3 camera.sh – this file will stream our camera over the internet.
Create the file first using the following command;
sudo nano camera.sh
Now either copy or type the code, available in the attached camera.sh file, into this empty file.
Congratulation the car is now complete!!!
How to play with the car/Run the code:
1. Power on the Pi (with HDMI cable connected to a monitor)
2. Once booted, open up Terminal
3. Change directory to PiCar (Type cd PiCar)
4. Run Car File (Type ‘sudo python main.py’)
5. The PiCar is now running and controllable via the wireless keyboard (Remember to remove the HDMI cable before driving the car!)
The below keys will drive the car:
W = Forward
S = Reverse
A = Turn Left
D = Turn Right
P = Stop
The Pi will also be semi-autonomous in the sense that if anything gets within 15cm of the front sensor then the PiCar will automatically reverse for a second.
Streaming the Camera (Optional):
On the Pi:
1. Open Terminal
2. Change directory to PiCar (Type cd PiCar)
3. Run camera.sh (Type ./camera.sh)
On the Client computer/smartphone:
1. Launch VLC
2. Click ‘File’ and then ‘Open Network Stream’
3. Type http://<YourIPAddressHere>:8554/
4. Click ‘Open’ and after a few seconds a new window will open with the stream.(There may be a few seconds lag)
Important: To find out your IP address enter the following command into the terminal:
hostname -I
For further help the fantastic guide below on how to stream the Pi’s camera:
http://www.mybigideas.co.uk/RPi/RPiCamera/
Next Steps/FutureObjectives:
1. Get main.py file to automatically run on start-up (So i don’t need to plug the Pi into a monitor overtime)
2. Create a python file that will perform the same action as our camera.sh bash script (using Picamera) and incorporate this into our main.py file.
3. Add additional ultrasonic sensors (Definitely one at the back and perhaps another at the front)
Any advice/feedback is extremely welcomed!
Further Reading:
The following is a list of guides/books/tutorials that I found extremely useful when looking to learn about the Raspberry Pi, Linux and electronics for the first time.
Raspberry Pi:
For more: Raspberry Pi Website – Guides on installation, configuration and Usage
Camera:
Guides to setting up and using the Raspberry Pi camera module
Linux:
20 Useful Commands for Linux Newbies
Installing applications
Guide on how to Install Applications on the Pi
Advanced Package (Application) Management Commands
Motors:
Source:Pi Car