Python Projects on Raspberry Pi
Python Projects on Raspberry Pi
Raspberry Pi
by Jason Van Schooneveld Jun 01, 2020 0 Comments intermediate python
Table of Contents
Getting to Know the Raspberry Pi
Raspberry Pi Board Overview
Raspberry Pi vs Arduino
Setting Up the Raspberry Pi
Required Hardware
Optional Hardware
So ware
Final Setup
Running Python on the Raspberry Pi
Using the Mu Editor
Editing Remotely Over SSH
Creating a python-projects Directory
Interacting With Physical Components
Electronic Components
GPIO Pins
Tactile Button
LED
Buzzer
Motion Sensor
Building a Motion-Activated Alarm System
Wiring
Code
Next Steps
Conclusion
The Raspberry Pi is one of the leading physical computing boards on the market. From hobbyists building DIY projects
Help
to students learning to program for the first time, people use the Raspberry Pi every day to interact with the world
around them. Python comes built in on the Raspberry Pi, so you can take your skills and start building your own
Raspberry Pi projects today.
Free Download: Get a sample chapter from Python Tricks: The Book that shows you Python's best practices
with simple examples you can apply instantly to write more beautiful + Pythonic code.
With all the features and capabilities that are packed into this small board, there’s no shortage of projects and use
cases for the Raspberry Pi.
Line-following robot
Home weather station
Retro gaming machine
Real-time object detection camera
Minecra server
Button-controlled music box
Media center
Remote experiments on the International Space Station
If you can think of a project that would benefit from having a credit card–sized computer attached to it, then someone
has probably used a Raspberry Pi to do it. The Raspberry Pi is a fantastic way to bring your Python project ideas to life.
Below is the board layout of the Raspberry Pi 4. While this layout is slightly di erent from previous models of the
Raspberry Pi, most of the connections are the same. The setup described in the next section should be the same for
both a Raspberry Pi 3 and a Raspberry Pi 4:
The Raspberry Pi 4 board contains the following components:
General-purpose input–output pins: These pins are used to connect the Raspberry Pi to electronic
components.
Ethernet port: This port connects the Raspberry Pi to a wired network. The Raspberry Pi also has WiFi and
Bluetooth built in for wireless connections.
Two USB 3.0 and two USB 2.0 ports: These USB ports are used to connect peripherals like a keyboard or
mouse. The two black ports are USB 2.0 and the two blue ports are USB 3.0.
AV jack: This AV jack allows you to connect speakers or headphones to the Raspberry Pi.
Camera Module port: This port is used to connect the o icial Raspberry Pi Camera Module, which enables the
Raspberry Pi to capture images.
HDMI ports: These HDMI ports connect the Raspberry Pi to external monitors. The Raspberry Pi 4 features two
micro HDMI ports, allowing it to drive two separate monitors at the same time.
USB power port: This USB port powers the Raspberry Pi. The Raspberry Pi 4 has a USB Type-C port, while older
versions of the Pi have a micro-USB port.
External display port: This port is used to connect the o icial seven-inch Raspberry Pi touch display for touch-
based input on the Raspberry Pi.
microSD card slot (underside of the board): This card slot is for the microSD card that contains the Raspberry
Pi operating system and files.
A little later in this tutorial, you’ll use the components above to set up your Raspberry Pi.
Raspberry Pi vs Arduino
People o en wonder what the di erence is between a Raspberry Pi and an Arduino. The Arduino is another device
that is widely used in physical computing. While there is some overlap in the capabilities of the Arduino and the
Raspberry Pi, there are some distinct di erences.
The Arduino platform provides a hardware and so ware interface for programming microcontrollers. A
microcontroller is an integrated circuit that allows you to read input from and send output to electronic components.
Arduino boards generally have limited memory, so they’re o en used to repeatedly run a single program that
interacts with electronics.
The Raspberry Pi is a general-purpose, Linux-based computer. It has a full operating system with a GUI interface that
is capable of running many di erent programs at the same time.
The Raspberry Pi comes with a variety of so ware preinstalled, including a web browser, an o ice suite, a terminal,
and even Minecra . The Raspberry Pi also has built-in WiFi and Bluetooth to connect to the Internet and external
peripherals.
For running Python, the Raspberry Pi is o en the better choice, as you get a full-fledged Python installation out of the
box without any configuration.
Required Hardware
The following hardware is required for the initial setup of your Raspberry Pi. If you end up connecting to your
Raspberry Pi over SSH, which you’ll look at later in this tutorial, then some of the hardware below will not be needed
a er the initial setup.
Monitor
You’ll need a monitor during the initial setup and configuration of the operating system. If you’ll be using SSH to
connect to your Raspberry Pi, then you won’t need the monitor a er setup. Make sure your monitor has an HDMI
input.
microSD Card
Raspberry Pi uses a microSD card to store the operating system and files. If you buy a Raspberry Pi kit, then it will
contain a preformatted microSD card for you to use. If you buy a microSD card separately, then you’ll need to format it
yourself. Look for a microSD card with at least 16GB of capacity.
HDMI Cables
You’ll need an HDMI cable to connect the Raspberry Pi to a monitor. Di erent Raspberry Pi models have di erent
HDMI cable requirements:
Depending on your model, you may need to purchase a special HDMI cable or adapter.
Power Supply
The Raspberry Pi uses a USB connection to power the board. Again, di erent Raspberry Pi models have di erent USB
connection and power requirements.
Below are the connection and power requirements for the di erent models:
USB-C Micro-USB
To avoid any confusion when selecting a power supply, it’s recommended that you use the o icial power supply for
your Raspberry Pi 4 or other model.
Optional Hardware
You can use a whole range of additional hardware with the Raspberry Pi to extend its capabilities. The hardware items
listed below are not required to use your Raspberry Pi but would be useful to have on hand.
Case
It’s nice to have a case for your Raspberry Pi to keep its components from being damaged during normal use. When
selecting a case, make sure that you purchase the correct type for your model of the Raspberry Pi.
Speakers
If you want to play music or sound from your Raspberry Pi, then you’ll need speakers. These can be any standard
speakers that have a 3.5 mm jack. You can connect the speakers to the Raspberry Pi using the AV jack on the side of
the board.
So ware
The operating system for the Raspberry Pi is stored on a microSD card. If your card did not come from an o icial
Raspberry Pi kit, then you’ll need to install the operating system on it.
There are multiple ways to set up the operating system on your Raspberry Pi. You can find out more about the
di erent installation options on the Raspberry Pi website.
In this section, you’ll look at two ways to install Raspbian, the o icially supported Raspberry Pi operating system,
which is based on Debian Linux.
You’ll be given two options when first loading the application: Choose OS and Choose SD Card. Select Choose OS first.
Note: There’s a chance that Windows may prevent the Raspberry Pi Imager from starting because it’s an
unrecognized application. If you receive a pop-up that says Windows protected your PC, then you can still run the
application by clicking More info and selecting Run anyway.
With the application running, click the Choose OS button and choose the first Raspbian option:
A er selecting the Raspbian operating system, you need to select the SD card you’re going to use. Make sure your
microSD card is inserted into your computer and click Choose SD Card, then select the SD card from the menu:
With the operating system and SD card selected, you can now click the Write button to begin formatting the SD card
and installing the operating system onto the card. This process may take a few minutes to complete:
Once formatting and installation are complete, you should see a message that says the operating system has been
written to the SD card:
You can eject the SD card from your computer. Raspbian is now installed on your SD card, and you’re ready to start
connecting hardware to the Raspberry Pi!
Once the ZIP file has been downloaded, unzip the contents to a location on your computer. You’ll be copying these
files onto the SD card shortly, but first you need to properly format the SD card.
You’ll use the o icial SD Memory Card Formatter from the SD Association. Head to the SD Association website to
download the formatter. Scroll to the bottom and download the SD formatter for either Windows or macOS:
Once the SD Memory Card Formatter is downloaded, you’re ready to format your SD card for use on the Raspberry Pi.
Note: Linux users can use fdisk to partition and format a microSD card to the required FAT32 disk format.
A er downloading the SD formatter, open the application. To format the SD card, you’ll need to do the following:
Before formatting the card, you’ll be asked to confirm the operation, as it will erase all data from the card. Click
Continue to begin formatting the SD card. It may take a few minutes to finish formatting:
Once the formatting has completed, you’ll need to copy the NOOBS files that you unzipped earlier over to the SD card.
Select all the files that you extracted earlier:
Drag them to the SD card:
Now that you’ve got NOOBS installed on your SD card, eject the card from your computer. You’re almost there! In the
next section, you’ll do the final setup for your Raspberry Pi.
Final Setup
Now that you have the microSD card and required hardware ready, the final step is to connect everything together
and configure the operating system. Let’s start o by connecting all the peripherals:
1. Insert the microSD card into the card slot on the bottom of the Raspberry Pi.
2. Connect the keyboard and mouse to any of the four USB ports.
3. Connect a display to one of the HDMI ports using an HDMI cable specific to your Raspberry Pi model.
4. Connect a power supply to the USB power port.
With the peripherals connected, go ahead and power on your Raspberry Pi to configure the operating system. If you
installed Raspbian with the Raspberry Pi Imager, then there’s nothing else for you to do. You can skip down to the next
section to complete the setup.
If you installed NOOBS on your SD card, then you’ll need to complete a couple more steps to install Raspbian on the
SD card:
Once the installation is complete, the Raspberry Pi will restart, and you’ll be booted into Raspbian to complete the
setup wizard.
Setup Wizard
On first boot, Raspbian provides a setup wizard to help you configure your password, set your locale, select a WiFi
network, and update the operating system. Go ahead and complete these steps as instructed.
Once you’ve completed the steps, restart the operating system and you’ll be ready to begin programming Python on
the Raspberry Pi!
You have many di erent options for writing Python on the Raspberry Pi. In this tutorial, you’ll look at two popular
choices:
Let’s start by looking at using the Mu editor to write Python on the Raspberry Pi.
When you open Mu for the first time, you’ll be given the option to select the Python mode for the editor. For the code
in this tutorial, you can select Python 3:
There’s a chance that Mu may not be preinstalled on your version of Raspbian. If Mu isn’t installed, then you can
always install it by going to the following file location:
This will open up a dialogue containing recommended so ware for your Raspberry Pi. Check the box next to Mu and
click OK to install it:
While Mu provides a great editor to get started with Python on the Raspberry Pi, you may want something more
robust. In the next section, you’ll connect to your Raspberry Pi over SSH.
Enable SSH
Before you can connect to the Raspberry Pi over SSH, you’ll need to enable SSH access inside the Raspberry Pi
Preferences area. Enable SSH by going to the following file path:
Once the configuration appears, select the Interfaces tab and then enable the SSH option:
You’ve enabled SSH on the Raspberry Pi. Now you need to get the IP address for the Raspberry Pi so you can connect
to it from another computer.
Shell
pi@raspberrypi:~ $ hostname -I
This will display the current IP address for your Raspberry Pi. With this IP address, you can now connect to your
Raspberry Pi remotely.
Shell
You’ll be prompted to enter the password you created when running the setup wizard during the Raspbian
installation. If you didn’t set a password, then the default password is raspberry. Enter the password, and a er it
connects you’ll see the Raspberry Pi command prompt:
Shell
pi@raspberrypi:~ $
Now that you know how to connect, you’re ready to start programming Python on the Raspberry Pi. You can get
started right away using the Python REPL:
Shell
pi@raspberrypi:~ $ python3
Python >>>
Let’s create a directory called python-projects where you can store Python code for your projects.
Using Mu
If you plan on using Mu to complete the projects in this tutorial, then you can use it now to create the python-projects
directory. To create this directory, you’ll want to do the following:
Over SSH
If you’d rather use SSH to access your Raspberry Pi, then you’ll use the command line to create the python-projects
directory.
Note: Since you’ll be accessing the Raspberry Pi command line, you’ll need to use a command-line text editor to
edit your project files.
Both nano and vim come preinstalled on Raspbian and can be used to edit the project files. You can also use VS
Code to remotely edit files on the Raspberry Pi, but some setup is required.
Let’s create the python-projects directory. If you aren’t currently logged into the Raspberry Pi, then use the IP address
of the Raspberry Pi to SSH into it from your computer:
Shell
Shell
pi@raspberry:~ $
By default, when you SSH into the Raspberry Pi, you’ll start in the /home/pi directory. Confirm this now by running pwd:
Shell
pi@raspberry:~ $ pwd
/home/pi
If for some reason you’re not in the /home/pi directory, then switch to it using cd /home/pi:
Shell
pi@raspberry:~/Desktop $ cd /home/pi
pi@raspberry:~ $ pwd
/home/pi
Shell
With the python-projects directory created, use cd python-projects to go into the directory:
Shell
pi@raspberry:~ $ cd python-projects
pi@raspberry:~/python-projects $
Great! You’re ready to start coding your first circuits using Python on the Raspberry Pi.
You’ll be using the gpiozero library that comes preinstalled on Raspbian. It provides an easy-to-use interface to
interact with a variety of GPIO devices connected to the Raspberry Pi.
Electronic Components
Before programming on the Raspberry Pi, you’ll need a few electronic components to build the projects in the
upcoming sections. You should be able to find each of the items below on Amazon or at your local electronics store.
Breadboard
A breadboard is an essential tool when building circuits. It allows you to quickly prototype your circuit without
having to solder components together.
Breadboards follow a general layout. On the right and le sides, two rails run the length of the breadboard. Every hole
on these rails is connected. Generally, these are designated positive (voltage, or VCC) and negative (ground, or GND).
On most breadboards, the positive rail is marked with a positive sign (+) and will have a red line running next to it.
The negative rail is marked with a negative sign (-) and has a blue line running next to it.
On the interior of the board, component rails run perpendicular to the positive and negative rails on the sides of the
breadboard. Each of these rails contains holes for placing components.
All holes in a single rail are connected. In the middle is a gutter separating the two sides of the breadboard. Rails on
opposite sides of the gutter are not connected.
In the diagram above, three colors are used to mark the di erent types of breadboard rails:
Later in this tutorial, you’ll use these di erent rails to build full circuits that connect to the Raspberry Pi.
Jumper Wires
Jumper wires allow you to prototype the connections of your circuit without having to solder paths between GPIO
pins and components. They come in three di erent types:
1. Male to male
2. Female to male
3. Female to female
It would be good to have at least ten to twenty of each type when you’re building your Raspberry Pi projects in
Python.
Other Components
Along with the breadboard and jumper wires, the projects in this tutorial will use the following components:
With the required components in hand, let’s take a look at how you can connect them to the Raspberry Pi using the
GPIO pins.
GPIO Pins
The Raspberry Pi features forty GPIO pins along the top edge of the board. You can use these GPIO pins to connect the
Raspberry Pi to external components.
The pin layout below shows the di erent types of pins and their locations. This layout is based on an overhead view
of the pins with the Raspberry Pi’s USB ports facing you:
1. GPIO: These are general-purpose pins that can be used for input or output.
2. 3V3: These pins supply a 3.3 V power source for components. 3.3 V is also the internal voltage that all GPIO pins
supply.
3. 5V: These pins supply a 5 V power source, the same as the USB power input that powers the Raspberry Pi. Some
components, such as the passive infrared motion sensor, require 5 V.
4. GND: These pins provide a ground connection for circuits.
5. ADV: These special-purpose pins are advanced and not covered in this tutorial.
In the next section, you’ll use these di erent pin types to set up your first component, a tactile button.
Tactile Button
For your first circuit, you’re going to connect a tactile button to the Raspberry Pi. A tactile button is an electronic
switch that, when pressed, closes a circuit. When a circuit is closed, the Raspberry Pi will register an ON signal. You
can use this ON signal to trigger di erent actions.
In this project, you’ll use a tactile button to run di erent Python functions based on the state of the button. Let’s start
by wiring the button to the Raspberry Pi:
1. Connect a female-to-male jumper wire from the Raspberry Pi’s GND pin to the negative rail of the breadboard.
2. Place a tactile button across the gutter in the middle of the breadboard.
3. Connect a male-to-male jumper wire from the negative rail of the breadboard to the row where the button’s
bottom-le leg is placed.
4. Connect a female-to-male jumper wire from the Raspberry Pi’s GPIO4 pin to the breadboard row where the
button’s bottom-right leg is placed.
Now that you’ve got your circuit wired up, let’s write the Python code to read input from the button.
Note: If you’re having trouble finding a specific pin, then make sure to reference the GPIO pin layout image
when building your circuits. You can also purchase a breakout board for easy breadboarding.
Inside the python-projects directory that you created earlier, save a new file called [Link]. If you’re using SSH to
access your Raspberry Pi, then create the file like this:
Shell
pi@raspberrypi:~/ cd python-projects
pi@raspberrypi:~/python-projects $ touch [Link]
If you’re using Mu, then create the file with the following steps:
Python
Create an instance of the Button class and pass the pin number as a parameter. In this case, you’re using the GPIO4
pin, so you’ll pass in 4 as the parameter:
Python
button = Button(4)
Next, define functions that will be called for the di erent button events that are available on a Button instance:
Python
def button_pressed():
print("Button was pressed")
def button_held():
print("Button was held")
def button_released():
print("Button was released")
The Button class has three event properties: .when_pressed, .when_held, and .when_released. These properties can be
used to hook up di erent event functions.
While the .when_pressed and .when_released properties are self-explanatory, .when_held requires a short explanation.
If a function is set to the .when_held property, then it will only be called if the button is pressed and held for a certain
amount of time.
The hold time for .when_held is determined by the .hold_time property on the Button instance. The default for
.hold_time is one second. You can override this by passing a float value when creating a Button instance:
Python
This will create a Button instance that will wait two and a half seconds a er the button is pressed and held before
calling the button_held() function.
Now that you know about the di erent event properties on Button, set each of these to their respective functions that
you defined earlier:
Python
button.when_pressed = button_pressed
button.when_held = button_held
button.when_released = button_released
Great! You’ve got your button events set up. The last thing you need to do is to call pause() at the end of the file.
Calling pause() is required to keep the program listening for the di erent events. If this wasn’t present, then the
program would run once and exit.
button = Button(4)
def button_pressed():
print("Button was pressed")
def button_held():
print("Button was held")
def button_released():
print("Button was released")
button.when_pressed = button_pressed
button.when_held = button_held
button.when_released = button_released
pause()
With the wiring complete and the code all set up, you’re ready to try out your first circuit. Inside of the python-
projects directory, run the program:
Shell
If you’re using Mu, first make sure the file is saved, then click Run to start the program.
The program is now running and listening for events. Press the button, and you should see the following in the
console:
Shell
Press and hold the button for at least one second, and you should see the following output:
Shell
Finally, when you release the button you should see the following:
Shell
Awesome! You’ve just wired and coded your first circuit using Python on the Raspberry Pi.
Since you used pause() in your code, you’ll need to manually stop the program. If you’re running the program in Mu,
then you can click Stop to quit the program. If you’re running this from the command line, then you can stop the
program with ⌃Ctrl + C .
With this first circuit under your belt, you’re ready to start controlling some other components.
LED
For your next circuit, you’ll use Python to blink an LED on and o every second. LED stands for light-emitting diode,
and these components produce light when a current is applied. You’ll find that they’re used everywhere in
electronics.
Every LED has two legs. The longer leg is the positive leg, or anode. The electrical current enters the LED through this
leg. The shorter leg is the negative leg, or cathode. The current exits the LED through this leg.
Current can only flow one direction through an LED, so make sure you’re connecting jumper wires to the proper leg of
the LED.
Here are the steps you’ll need to take to wire this circuit:
1. Connect a female-to-male jumper wire from the Raspberry Pi’s GND pin to the negative rail of the breadboard.
2. Place an LED into two holes on the breadboard that are next to each other but not in the same row.
3. Place the longer, positive leg of the LED into the hole on the right side.
4. Place the shorter, negative leg of the LED into the hole on the le side.
5. Place one end of a 330 Ω resistor into a hole in the same breadboard row as the negative leg of the LED.
6. Place the other end of the resistor into the negative rail of the breadboard
7. Connect a female-to-male jumper wire from the Raspberry Pi’s GPIO4 pin to a hole in the same breadboard row
as the positive leg of the LED.
If the wiring looks good, then you’re ready to write some Python to get the LED blinking. Start by creating a file for this
circuit inside of the python-projects directory. Call this file [Link]:
Shell
In this code, you’ll create an instance of the LED class and call its .blink() method to make the LED blink on and o .
The .blink() method has a default timeout of one second. The LED will continue to blink on and o every second
until the program is exited.
Start by importing LED from the gpiozero module and pause from the signal module:
Python
Next, create an instance of LED called led. Set the GPIO pin to 4:
Python
led = LED(4)
Call the .blink() method on led:
Python
[Link]()
Finally, add a call to pause() to make sure the program doesn’t exit:
Python
pause()
Python
led = LED(4)
[Link]()
pause()
Save the file and run it to see the LED blink on and o :
Shell
The LED should now be blinking on and o every second. When you’re done admiring your Python code in action,
stop the program with ⌃Ctrl + C or Stop in Mu.
Now you know how to control an LED with Python on the Raspberry Pi. For the next circuit, you’ll be using Python to
produce sound from the Raspberry Pi.
Buzzer
In this circuit, you’ll be wiring an active piezo buzzer to the Raspberry Pi. A piezo buzzer emits a tone when current is
applied. Using this component, your Raspberry Pi will be able to generate sound.
Like LEDs, a buzzer has a positive and negative leg. The positive leg of the buzzer will either be longer than the
negative leg or there will be a positive sign (+) on the top of the buzzer showing which leg is the positive leg.
1. Place a buzzer on the breadboard, noting the placement of the buzzer’s positive leg.
2. Connect a female-to-male jumper wire from the Raspberry Pi’s GND pin to a hole in the same breadboard row
as the negative leg of the buzzer.
3. Connect a female-to-male jumper wire from the Raspberry Pi’s GPIO4 pin to a hole in the same breadboard row
as the positive leg of the buzzer.
Shell
In this code, you’ll create an instance of the Buzzer class and call it’s .beep() method to make the buzzer beep on and
o . The .beep() method’s first two parameters are on_time and off_time. These parameters take a float value to set
how long the buzzer should beep on and o . The default value for both is one second.
Start by importing Buzzer from the gpiozero module and pause from the signal module:
Python
Next, create an instance of Buzzer called buzzer. Set the GPIO pin to 4:
Python
buzzer = Buzzer(4)
Call the .beep() method on buzzer. Set the on_time and off_time parameters to 0.5. This will make the buzzer beep
every half second:
Python
[Link](0.5, 0.5)
Finally, add a call to pause() to make sure the program doesn’t exit:
Python
pause()
buzzer = Buzzer(4)
[Link](0.5, 0.5)
pause()
Save the file and run it to hear the buzzer beep on and o every half second:
Shell
You should hear the buzzer sound on and o until you stop the program with ⌃Ctrl + C or Stop in Mu.
Note: If you’re using Mu, then there’s a chance that when you stop the program the tone will continue. To stop
the sound, remove the GND wire to break the circuit.
You may also need to restart Mu if the tone continues when you reconnect the GND wire.
Great! So far you’ve learned how to control three di erent types of electronic components with Python on the
Raspberry Pi. For the next circuit, let’s take a look at a slightly more complex component.
Motion Sensor
In this circuit, you’ll be connecting a passive infrared (PIR) motion sensor to the Raspberry Pi. A passive infrared
motion sensor detects any motion in its field of vision and sends a signal back to the Raspberry Pi.
You can make adjustments using two dials on the side of the sensor. You’ll know which dials they are because they’ll
have a cross-shaped indentation in the center, which can be adjusted with a Phillips-head screwdriver.
The image below shows these dials on the side of the motion sensor:
As the image shows, the le dial sets the signal timeout and the right dial sets the sensor sensitivity. You can turn
these dials clockwise or counterclockwise to adjust them:
You can adjust these based on your project needs, but for this tutorial turn both dials counterclockwise all the way.
This will set them to their lowest values.
Note: Occasionally, a motion sensor and a Raspberry Pi 3 will not work together correctly. This results in
occasional false positives from the sensor.
If you’re using a Raspberry Pi 3, then make sure to move the sensor as far from the Raspberry Pi as possible.
Once you’ve adjusted the motion sensor, you’re ready to set up the wiring. The motion sensor’s design doesn’t allow it
to easily connect to a breadboard. You’ll need to connect the Raspberry Pi’s GPIO pins directly to the pins on the
motion sensor with jumper wires.
The image below shows the placement of the pins on the underside of the motion sensor:
1. Connect a female-to-female jumper wire from the Raspberry Pi’s 5V pin to the sensor’s VCC pin.
2. Connect a female-to-female jumper wire from the Raspberry Pi’s GPIO4 pin to the sensor’s OUT pin.
3. Connect a female-to-female jumper wire from the Raspberry Pi’s GND pin to the sensor’s GND pin.
Shell
The code for this circuit is going to be similar to the button circuit you made previously. You’ll create an instance of
the MotionSensor class and pass in the GPIO pin number 4 as the parameter. You’ll then define two functions and set
them to the .when_motion and .when_no_motion properties on the MotionSensor instance.
Python
motion_sensor = MotionSensor(4)
def motion():
print("Motion detected")
def no_motion():
print("Motion stopped")
print("Readying sensor...")
motion_sensor.wait_for_no_motion()
print("Sensor ready")
motion_sensor.when_motion = motion
motion_sensor.when_no_motion = no_motion
pause()
motion() is set to the .when_motion property and is called when the sensor detects motion. no_motion() is set to the
.when_no_motion property and is called when motion has stopped for a certain period. This time is determined by the
timeout dial on the side of the sensor.
You’ll notice that before the .when_motion and .when_no_motion properties are set, there is a call to
.wait_for_no_motion() on the MotionSensor instance. This method will pause the execution of the code until the
motion sensor no longer detects any motion. This is so that the sensor will ignore any initial motion that may occur as
the program is starting up.
Note: Motion sensors can sometimes be too sensitive or not sensitive enough. If you see inconsistent results in
the console when running the code above, then make sure to check that everything is wired correctly. You may
also need to adjust the sensitivity dial on your sensor.
If your results are delayed in the console, then try adjusting down the .threshold property on the MotionSensor
instance. The default value is 0.5:
Python
This will decrease the amount of motion required to make the sensor active. For more information on the
MotionSensor class, see the gpiozero documentation.
Save the code and run it to try out your motion detection circuit:
Shell
Wave your hand in front of the sensor. When motion is first detected, motion() is called and the following is displayed
in the console:
Shell
Motion detected
Now stop waving your hand in front of the sensor. A er a few seconds, the following will be displayed:
Shell
Motion stopped
Great! You can now detect motion with your Raspberry Pi. Once you’re done waving at your Raspberry Pi, go ahead
and hit ⌃Ctrl + C in the command line or press Stop in Mu to terminate the program.
With this final circuit, you’ve learned how to use Python on the Raspberry Pi to control four di erent components. In
the next section, you’ll tie all of this together in a full project.
In this project, you’ll build a motion-activated alarm system that will flash an LED and sound an alarm when it detects
motion in a room. On top of this, you’ll use Python to save a timestamp to a CSV file detailing each time motion
occurs.
Wiring
Here are the steps to complete the wiring:
1. Connect female-to-male jumper wires from the Raspberry Pi’s 5V and GND pins to the positive and negative
rails on the side of the breadboard.
2. Place an LED on the breadboard and connect the Raspberry Pi’s GPIO14 pin to the LED with a female-to-male
jumper wire.
3. Connect the LED’s negative leg to the breadboard’s negative rail through a 330 Ω resistor.
4. Place a buzzer on the breadboard and connect the Raspberry Pi’s GPIO15 pin to the buzzer with a female-to-
male jumper wire.
5. Connect the buzzer’s negative leg to the breadboard’s negative rail with a male-to-male jumper wire.
6. Connect a female-to-male jumper wire from the breadboard’s positive rail to the sensor’s VCC pin.
7. Connect a female-to-female jumper wire from the Raspberry Pi’s GPIO4 pin to the sensor’s OUT pin.
8. Connect a female-to-male jumper wire from the breadboard’s negative rail to the sensor’s GND pin.
Okay, now that you have the circuit wired up, let’s dig into the Python code to set up your motion-activated alarm
system.
Code
As usual, start by creating a file for this project inside the python-projects directory. For this project, call this file
motion_detector.py:
Shell
The first thing you want to do is import the csv module so you can save a timestamp when motion is detected. Also,
import Path from the pathlib module so you can reference your CSV file:
Python
import csv
from pathlib import Path
Next, import datetime from the datetime module so you can create a timestamp of the motion event:
Python
Finally, import the required component classes from gpiozero as well as pause from the signal module:
Python
With the imports ready, you can set up the three electronic components you’ll be using. Create instances of the LED,
Buzzer, and MotionSensor classes. For each of these, pass in their pin numbers as parameters:
Python
led = LED(14)
buzzer = Buzzer(15)
motion_sensor = MotionSensor(4)
Next, define the location of a CSV file that will store a timestamp each time motion is detected. You’ll call it
detected_motion.csv. Create a dictionary to hold the timestamp values that will be written to the CSV:
Python
output_csv_path = Path("detected_motion.csv")
motion = {
"start_time": None,
"end_time": None,
}
Create a method that saves the timestamp data to a CSV file. When the file is first created, a header row is added:
Python
def write_to_csv():
first_write = not output_csv_path.is_file()
Add a call to print() so you can watch the events occur while the program is running:
Python
def start_motion():
[Link](0.5, 0.5)
[Link](0.5, 0.5)
motion["start_time"] = [Link]()
print("motion detected")
Then define an end_motion() function that will have the following behaviors:
You’ll also check that a motion["start_time"] value exists before running any of the other code. You only want to write
to the CSV if a start_time timestamp was recorded:
Python
def end_motion():
if motion["start_time"]:
[Link]()
[Link]()
motion["end_time"] = [Link]()
write_to_csv()
motion["start_time"] = None
motion["end_time"] = None
print("motion ended")
Python
print("Readying sensor...")
motion_sensor.wait_for_no_motion()
print("Sensor ready")
Python
motion_sensor.when_motion = start_motion
motion_sensor.when_no_motion = end_motion
Finally, end the code with a call to pause() to keep the program running. The full Python code should look like this:
Python
import csv
from pathlib import Path
from datetime import datetime
from gpiozero import LED, Buzzer, MotionSensor
from signal import pause
led = LED(14)
buzzer = Buzzer(15)
motion_sensor = MotionSensor(4)
output_csv_path = Path("detected_motion.csv")
motion = {
"start_time": None,
"end_time": None,
}
def write_to_csv():
first_write = not output_csv_path.is_file()
def start_motion():
[Link](0.5, 0.5)
[Link](0.5, 0.5)
motion["start_time"] = [Link]()
print("motion detected")
def end_motion():
if motion["start_time"]:
[Link]()
[Link]()
motion["end_time"] = [Link]()
write_to_csv()
motion["start_time"] = None
motion["end_time"] = None
print("motion ended")
print("Readying sensor...")
motion_sensor.wait_for_no_motion()
print("Sensor ready")
motion_sensor.when_motion = start_motion
motion_sensor.when_no_motion = end_motion
pause()
Save the file and run it to test out your new motion detector alarm:
Shell
Now if you wave your hand in front of the motion detector, then the buzzer should start beeping and the LED should
flash. If you stop moving for a few seconds, then the alarm should stop. In the console you should see the following:
Shell
Go ahead and stop the program with Stop in Mu or ⌃Ctrl + C . Let’s check out the CSV file that was generated:
Shell
As you can see, the timestamps for the motion’s start_time and end_time have been added to the CSV file.
Congratulations! You’ve created a non-trivial electronics project with Python on the Raspberry Pi.
Next Steps
You don’t have to stop here. There are many ways you could improve this project by leveraging the capabilities of
Python on the Raspberry Pi.
Connect the Raspberry Pi Camera Module and have it take a photo whenever motion is detected.
Connect a speaker to the Raspberry Pi and use PyGame to play a sound file to intimidate an intruder.
Add a button to the circuit to allow a user to manually toggle motion detection on or o .
There are many ways to upgrade this project. Let us know what you come up with!
Conclusion
The Raspberry Pi is an amazing computing device that just keeps getting better. It’s packed with features that make it
the go-to device for physical computing.
Python is the perfect complement to the Raspberry Pi, and with the skills you’ve learned you’re ready to tackle cool
and innovative physical computing projects. We can’t wait to hear what you build!
Jason is a so ware developer based in Taipei. When he's not tinkering with electronics or buildin
Django web apps, you can find him hiking the mountains of Taiwan or brushing up on his Chinese
Table of Contents
Getting to Know the Raspberry Pi
Setting Up the Raspberry Pi
Running Python on the Raspberry Pi
Interacting With Physical Components
Building a Motion-Activated Alarm System
Conclusion
Joanna Jacob
Real Python Comment Policy: The most useful comments are those written with the goal of learning
from or helping out other readers—a er reading the whole article and all the earlier comments.
Complaints and insults generally won’t make the cut here.
What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use?
Leave a comment below and let us know.
Python eval(): Evaluate Defining Your Own Python Scope & the
Expressions … Python Function – … LEGB Rule: …
a month ago • 1 comment 4 months ago • 4 comments 3 months ago • 6 comments
In this step-by-step tutorial, In this tutorial, you'll learn In this step-by-step tutoria
you'll learn how Python's how to define and call your you'll learn what scopes ar
eval() works and how to … own Python function. … how they work, and how …
Keep Learning