The Raspberry Pi has General Purpose Input Output pins. This guide will give an short insight into controlling the GPIO pins on the Raspberry Pi using a Python library called GPIOzero.
Your Raspberry Pi is more than just a small computer, it is a hardware prototyping tool! The RPi has bi-directional I/O pins, which you can use to drive LEDs, spin motors, or read button presses. To drive the RPi’s I/O lines requires a bit or programming. You can use a variety of programing languages, but we decided to use a really solid, easy language for driving I/O: Python.
9.1. Materials needed¶
9.2. GPIO Pinout¶
Raspberry has its GPIO over a standard male header on the board. From the first models to the latest, the header has expanded from 26 pins to 40 pins while maintaining the original pinout.
There are (at least) two, different numbering schemes you may encounter when referencing Pi pin numbers:
- Broadcom (SoC) chip-specific pin numbers.
- P1 physical pin numbers.
You can use either number-system, but when you are programming how to use the pins, it requires that you declare which scheme you are using at the very beginning of your program. We will see this later.
The next table shows all 40 pins on the P1 header, including any particular function they may have, and their dual numbers:
In the next table, we show another numbering system along with the ones we showed above: Pi pin header numbers and element14 given names, wiringPi numbers, Python numbers, and related silkscreen on the wedge. The Broadcom pin numbers in the table are related to RPi Model 2 and later only.
This table shows that the RPi not only gives you access to the bi-directional I/O pins, but also
There is a useful online guide for finding the assignment and numbering of each pin, along with other guides, that you may find useful at: pinout.xyz
9.2.1. Analog vs. Digital¶
Before starting with our practise, we will revise the difference between analog and digital signals. Both are used to transmit information, usually through electric signals. In both these technologies, the information, such as any audio or video, is transformed into electric signals. The difference between analog and digital:
- In analog technology, information is translated into electric pulses of varying amplitude.
- In digital technology, translation of information is into binary format (zero or one) where each bit is representative of two distinct amplitudes.
22.214.171.124. Comparison chart¶
|Signal||Analog signal is a continuous signal which represents physical measurements.||Digital signals are discrete time signals generated by digital modulation.|
|Waves||Denoted by sine waves.||Denoted by square waves.|
|Representation||Uses continuous range of values to represent information.||Uses discrete or discontinuous values to represent information.|
|Example||Human voice in air, analog electronic devices.||Computers, CDs, DVDs, and other digital electronic devices.|
|Technology||Analog technology records waveforms as they are.||Samples analog waveforms into a limited set of numbers and records them.|
|Data transmissions||Subjected to deterioration by noise during transmission and write/read cycle.||Can be noise-immune without deterioration during transmission and write/read cycle.|
|Response to Noise||More likely to get affected reducing accuracy.||Less affected since noise response are analog in nature.|
|Flexibility||Analog hardware is not flexible.||Digital hardware is flexible in implementation.|
|Uses||Can be used in analog devices only. Best suited for audio and video transmission.||Best suited for Computing and digital electronics.|
|Bandwidth||Analog signal processing can be done in real time and consumes less bandwidth.||There is no guarantee that digital signal processing can be done in real time and consumes more bandwidth to carry out the same information.|
|Memory||Stored in the form of wave signal.||Stored in the form of binary bit.|
|Power||Analog instrument draws large power.||Digital instrument draws negligible power.|
|Cost||Low cost and portable.||Cost is high and not easily portable.|
|Impedance||Low||High order of 100 megaohms|
|Errors||Analog instruments usually have a scale which is cramped at lower end and give considerable observational errors.||Digital instruments are free from observational errors like parallax and approximation errors.|
9.4. LED PWM¶
Use the same layout for the electronics as before.
9.4.1. What is PWM?¶
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (3.3 Volts for RPi and 5 Volts for Arduino) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of “on time” is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.
For more information check out this link.
9.4.2. The Code¶
Repeat the same steps of “Blink” to upload the code below, this time call the file led-pwm.py and save it in the
code folder that we have previously created. It’s up to you to make the code executable or not.
#!/usr/bin/env python from gpiozero import PWMLED from time import sleep led = PWMLED(17) while True: led.value = 0 # off sleep(1) led.value = 0.5 # half brightness sleep(1) led.value = 1 # full brightness sleep(1)
9.4.3. Understanding “LED PWM” code¶
The main difference here is that we are using the class PWMLED instead of the class LED. The PWMLED class has an extra parameter that we can tweak which is
value indicates the duty cycle of this PWM device.
0.0 is off,
1.0 is fully on. Values in between may be specified for varying levels of power in the device.
9.6. Combining Everything¶
Now we challenge you to combine all the previous three scripts to create one. Make the script in order that:
- when the button is pressed one of the two LEDs fades to 25% of its brightness and the other one blinks once
- when the button is released the PWM LED goes back to 100% brightness.
9.6.1. Hardware Setup¶
We start assembling the circuit as shown in the diagram below.