How to build a robot with Raspberry Pi

Boot your Raspberry Pi to the desktop and open a terminal, you can find the icon in the menu bar at the top left corner of the screen.

How to build a robot with Raspberry Pi

The Raspbian desktop

In the LXTerminal type the following and press Enter to run:

$ sudo raspi-config

Using the arrow keys navigate to Advanced Options and press Enter. In the Advanced menu navigate to the SSH Server option, press Enter and in the new screen choose to Enable the SSH server.

Exit from the menus and reboot your Raspberry Pi. Reboot back to the desktop and open another LXTerminal and type the following for your IP address and write the address down:

$ hostname -I

In the same terminal type the following to launch the Python 3 editor with superuser powers:

$ sudo idle3 &.

We'll start our code by importing two libraries, the first enables our code to talk to the GPIO pins on our Raspberry Pi while the second provides the time library:

import RPi.GPIO as GPIO

import time

When using the GPIO pins we will refer to them using their Broadcom pin numbering and we must, in turn, configure our code to use those numbers with GPIO.setmode(GPIO.BCM).

Rather than refer to each pin throughout our code we shall create four variables to store the GPIO pin connected to each of the inputs on the L298N:

fwdleft = 17

fwdright = 18

revleft = 22

revright = 23

In order to use each GPIO pin we need to instruct the code what each pin will be: an input or output. As we will be sending current from the GPIO pins they will be an output.

So using a list, known in other languages as an array, and a for loop, we shall iterate over each item in the list, which are our variables, and configure each GPIO pin as follows:

motors = [fwdleft,fwdright,revleft,revright]

for item in motors:

[Tab] GPIO.setup(item, GPIO.OUT)

Not that where we type '[Tab]' hit the Tab key on your keyboard to make the code indented for that line - this is important when using Python.

TOPICS
Latest in Raspberry Pi
white keyboard PC with mouse and book
The Raspberry Pi 500 is a state-of-the-art keyboard PC...just like the Tandy 1000 in the 1980s
Raspberry Pi strapped to a satellite takes stunning picture of Earth
Kali Linux on Pi 4
Raspberry Pi 4 gets Kali Linux, a distro aimed at ethical hackers
Worried about Alexa spying on you? This Raspberry Pi smart speaker for kids puts privacy first
How to install Ubuntu on the Raspberry Pi
New Raspberry Pi 3 Model A+ is somehow even smaller and cheaper
Latest in How Tos
Mobile phone with logo of video game distribution platform Steam operated by Valve on screen in front of web page
How to gift a game on Steam
Venezuela's forward #09 Jhonder Cadiz celebrates after scoring during the 2026 FIFA World Cup South American qualifiers football match between Ecuador and Venezuela, at the Rodrigo Paz Delgado stadium in Quito, on March 21, 2025 ahead of Venezuela vs Peru
Venezuela vs Peru live stream: how to watch today's FIFA World Cup 2026 qualifier anywhere online
The cast of Alone Australia season 3, standing in a knee-high body of water
How to watch Alone Australia season 3 online – stream the survival show from anywhere
Pinterest
How to post on Pinterest
ChatGPT app on an iPhone
5 things you should ask ChatGPT today – oh, and 1 you should never ask it!
This City Is Ours
How to watch This City Is Ours online – stream Sean Bean crime drama from anywhere