Lesson 4 – Breathing LED’s
Booting up the Raspberry Pi
- This section assumes that you have read through “Lesson 0 – Setup” and have a working Raspberry Pi you can connect to remotely using SSH or VNC.
- If that’s not the case please head back to “Lesson 0 – Setup” and work through the instructions provided.
- Let’s now get started and boot up the Raspberry Pi.
- Grab a good quality USB cable and a USB power adaptor (2.5A).
- Plug one end of the USB cable into the plug and the other microUSB end into the Raspberry Pi.
- This should now power up the Raspberry Pi.
- Once the Raspberry Pi has booted up, please ensure that it is able to connect it to the network so that you can access it over VNC.
- If you are using a local monitor connected to the Raspberry Pi, you are all sorted.
- Else get connected to the Raspberry Pi using VNC.
Important Concepts
01 / What is PWM or Pulse Width Modulation – Pulse Width Modulation (PWM) is a fancy term for describing a type of digital signal. Pulse width modulation is used in a variety of applications including sophisticated control circuitry. A common use of PWM is to control dimming of RGB LEDs or to control the direction of a servo motor. We can accomplish a range of results in both applications because pulse width modulation allows us to vary how much time the signal is high in an analog fashion. While the signal can only be high (usually 5V) or low (ground) at any time, we can change the proportion of time the signal is high compared to when it is low over a consistent time interval.
Duty Cycle – When the signal is high, we call this “on time”. To describe the amount of “on time” , we use the concept of duty cycle. Duty cycle is measured in percentage. The percentage duty cycle specifically describes the percentage of time a digital signal is on over an interval or period of time. This period is the inverse of the frequency of the waveform. If a digital signal spends half of the time on and the other half off, we would say the digital signal has a duty cycle of 50% and resembles an ideal square wave. If the percentage is higher than 50%, the digital signal spends more time in the high state than the low state and vice versa if the duty cycle is less than 50%. Here is a graph that illustrates these three scenarios:
100% duty cycle would be the same as setting the voltage to 5 Volts (high). 0% duty cycle would be the same as grounding the signal. Read more at – https://learn.sparkfun.com/tutorials/pulse-width-modulation.
Let’s put together the circuit
- Our next tutorial is called, “Breathing LED’s”.
- We will start off this tutorial by first putting together the circuit using a breadboard.
- We will wire up an LED, connect it up to the Raspberry Pi through a 220 Ohm resistors, power it up and write some python code.
In this lesson, we will use the Raspberry Pi to gradually increase and decrease brightness of an LED. We will generate a PWM signal and vary the duty time of the signal to change the rate at which the LED brightness increases and decreases. We use a 220 Ohm resistor to limit the current flowing through the LED. Now, let’s get started.
Components required for this tutorial include –
- 1 Raspberry Pi
- 1 Breadboard
- 1 Raspberry Pi Cobbler / T-Extension Board
- 1 40-Pin Cable to connect the Raspberry Pi Cobbler / T-Extension Board to the Raspberry Pi
- 1 LED
- 1 Resistor (220Ω)
- Dupont jumper wires
We will thus use the Pulse Width Modulation approach to gradually increase, decrease the brightness of an LED. Lets wire up your circuit based on the fritzing diagram below.
PLEASE NOTE – Please make sure you have disconnected your breadboard from the Raspberry Pi before commencing build of the circuit. Once you have put the circuit together, get someone around you to review the circuit and confirm that the connections are proper before you proceed and power up the breadboard.
Wiring Up
In this experiment, we have connected 1 x 220Ω resistor to the anode (the longer leg of the LED is the Anode and the shorter leg of the LED is the Cathode) of the LED, followed by the other end of each of the resistor to our 3.3 V power source on the breadboard (using the Raspberry Pi Cobbler / T-Extension Board) and finally connected the connect the cathode (the shorter leg) of the LED to pins B18 on the Raspberry Pi. The fritzing diagram below provides a view of all of the connections that need to be made for this tutorial.
Your connections should look like the following –
- Setup the Raspberry Pi Cobbler / T-Extension Board on the Breadboard.
- See the fritzing diagram below to understand what a suitable setup might look like.
- Note that one pair of the Ground/3.3V connects to the upper two (horizontally connected) rows of the breadboard.
- The second pair of the Ground/5V connects to the lower two (horizontally connected) rows of the breadboard.
- Wire up the 220Ω resistor to the anode (the longer leg of the LED is the Anode and the shorter leg of the LED is the Cathode) of the LED
- Wire up the other end of the resistor to our 3.3 V power source on the breadboard (using the Raspberry Pi Cobbler / T-Extension Board)
- Wire up the cathode (the shorter leg) of each of the LED to to pin B18 on the Raspberry Pi.
How Does It Work
We can see from the schematic diagram below that the Anode of the LED connects to a 220 Ohm current-limiting resistor and then to 3.3V power source (on the Raspberry Pi using the Raspberry Pi Cobbler / T-Extension Board). Therefore, to turn on the LED, we need to program the B18 pin on the Raspberry Pi to be low (0 Volt). This can be achieved using a few different programming languages but in our case we will be using Python and we will use the Pulse Width Modulation approach to gradually increase, gradually decrease brightness of the LED in a continuous cycle.
When you are done implementing the code, why not vary the PWM duty cycle and come up with some interesting lighting patterns out. Build on the example given and try creating different lighting patterns.
Here’s what the real world project will look like once you have wired up all the relevant components i.e. Raspberry Pi 3B+, Raspberry Pi Cobbler / T-Extension Board, breadboard, 1 x 220 Ohm resistor and 1 x LED.
Let’s Write Some Python Code
Open up the Thonny editor on your Raspberry Pi and let’s start putting together some code……
#!/usr/bin/env python import RPi.GPIO as GPIO import time # Set #17 as LED pin LedPin = 18 def print_message(): print ("========================================") print ("| Breath LED |") print ("| ------------------------------ |") print ("| LED connect to GPIO0 |") print ("| |") print ("| Make LED breath |") print ("| |") print ("| SunFounder|") print ("========================================\n") print 'Program is running...' print 'Please press Ctrl+C to end the program...' raw_input ("Press Enter to begin\n") def setup(): global pLed # Set the GPIO modes to BCM Numbering GPIO.setmode(GPIO.BCM) # Set LedPin's mode to output, # and initial level to low (0v) GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.LOW) # Set pLed as pwm output and frequece to 1KHz pLed = GPIO.PWM(LedPin, 1000) # Set pLed begin with value 0 pLed.start(0) def main(): print_message() # Set increase/decrease step step =2 # Set delay time. delay = 0.05 while True: # Increase duty cycle from 0 to 100 for dc in range(0, 101, step): # Change duty cycle to dc pLed.ChangeDutyCycle(dc) print " ++ Duty cycle: %s"%dc time.sleep(delay) time.sleep(1) # decrease duty cycle from 100 to 0 for dc in range(100, -1, -step): # Change duty cycle to dc pLed.ChangeDutyCycle(dc) print " -- Duty cycle: %s"%dc time.sleep(delay) #time.sleep(1) def destroy(): # Stop pLed pLed.stop() # Turn off LED GPIO.output(LedPin, GPIO.HIGH) # Release resource GPIO.cleanup() # If run this script directly, do: if __name__ == '__main__': setup() try: main() # When 'Ctrl+C' is pressed, the child program # destroy() will be executed. except KeyboardInterrupt: destroy()
See a PDF copy of the tutorial here – <Link>
About the Raspberry Pi
The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.
The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.
You can read more about the Raspberry Pi here – RaspberryPi.org.
Prerequisites
- This development track is based on the Rasbperry Pi and the SunFounder Super Starter Kit v3.0 for the Raspberry Pi.
- You will need access to both the Raspberry Pi 3 B and the electronics components part of the SunFounder Super Starter Kit v3.0 for the Raspberry Pi kit to be able to work on these tutorials.
- If you haven’t purchased the Raspberry Pi 3 B yet please head over to our store and purchase one now. You can pick up the SunFounder Super Starter Kit v3.0 for the Raspberry Pi from SunFounder’s website.
- Depending on where you live you might also be able to pick up the Raspberry Pi and SunFounder Super Starter Kit v3.0 for the Raspberry Pi at your local electronics hobby store.
- You can read more about the Raspberry Pi here – RaspberryPi.org.