Dirt Simple Circuit
The Breadboard

Grab Stuff
Wiring
Power first
Then the components
How electrons flow
Fritzing
A cleaner and standardized way of representing circuits. Also what we’ll use from now on.
Raspberry Pi
We’ll use model Zero 2 W, please grab one and turn it on.

It’s just a tiny computer. Question on components?
GPIO Pins
Power From the Pins

Coding with Python
Hello World!
Let’s run our very first Python code
print( "Hello World!" )
Variables
my_variable = "Hello World!"
print( my_variable )
Operating on Variables
my_variable = "Hello World!"
print( my_variable )
# string replace
my_variable = my_variable.replace( "World", "Universe" )
print( my_variable )
# convert to upper case
my_variable = my_variable.upper()
print( my_variable )
# substring
my_variable = my_variable[0:5]
print( my_variable )
# string concatenation
another_variable = "Blobfish"
print( my_variable + " " + another_variable + "!" )
Numbers
# this is an integer
counter = 5
counter = counter + 15
print( counter )
# this is a float
fine_counter = 20.0
fine_counter = fine_counter / 17
print( fine_counter )
Printing Numbers
# this is an integer
counter = 5
counter = counter + 15
print( "the current value of counter is: " + counter )
# this is a float
fine_counter = 20.0
fine_counter = fine_counter / 17
print( "the current value of fine_counter is: " + fine_counter )
What’s wrong with this code?
Loops
Take a good look at the indentation.
print( "before the loop" )
for counter in range( 0, 10 ):
print( "inside the loop, counter value is: " + str(counter) )
print( "outside the loop" )
What does the “range( 0, 10 )” function return?
Conditions
for counter in range( 0, 10 ):
print( "counter value is: " + str(counter) )
if (counter % 2) == 0:
print( " which is an even number!" )
How would you check to see if the counter is even AND greater than 5?
Functions
def is_prime( n ):
# make sure n is a positive integer
n = abs(int(n))
# 0 and 1 are not primes
if n < 2:
return False
# 2 is the only even prime number
if n == 2:
return True
# all other even numbers are not primes
if not n & 1:
return False
# range starts with 3 and only needs to go up the squareroot of n
# for all odd numbers
for x in range(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True
for counter in range( 0, 10 ):
print( "counter value is: " + str(counter) )
if is_prime( counter ):
print( " which is a prime number!" )
What is the keyword for defining a function?
Modules
import math
print( math.sqrt(16) )
Python Control of the GPIO Pins
# import needed modules
import time
import RPi.GPIO as GPIO
led = 4 # connect the LED to this pin on the GPIO
# set the mode for the pin
GPIO.setmode( GPIO.BCM )
# set our LED to output
GPIO.setup( led, GPIO.OUT )
print( "turning LED on" )
GPIO.output( led, 1 )
# leaving LED on for 5 seconds
time.sleep( 5 )
print( "turning LED off" )
GPIO.output( led, 0 )
# the program is finished, we put things back in their original state
GPIO.cleanup()
Experiment on the Persistence of Vision
Let’s talk about frequency a bit.
Try to adjust the frequency so the LED blinks so fast you don’t see it blinking anymore? Does it mean it doesn’t blink anymore? Is it as bright as it we had just turned it on without blinking fast?
# import needed modules
import time
import RPi.GPIO as GPIO
led = 4 # connect the LED to this pin on the GPIO
# set the mode for the pin
GPIO.setmode( GPIO.BCM )
# set our LED to output
GPIO.setup( led, GPIO.OUT )
frequency = 10 # in Hertz (Hz)
experiment_duration = 5 # in Seconds (s)
for i in range( 0, experiment_duration*frequency ):
time.sleep( (1/frequency)/2 )
GPIO.output( led, 1 )
time.sleep( (1/frequency)/2 )
GPIO.output( led, 0 )
# the program is finished, we put things back in their original state
GPIO.cleanup()
Please Take a Moment
To appreciate how you went from:
- the simplest battery powered LED circuit
- to powering with the Pi instead
- and finally, controlling it with a fully fledged programming language
We now have programmatic control over our LED, things are about to get fun.
PWM & Fancy LED
PWM stands for Pulse Width Modulation, it’s a type of signal that isn’t just “on” or “off”. It’s oscillating between the 2 with a certain frequency, and a duty cycle.
RGB LED Circuit
Please grab:
- 1 RGB LED
- 3 more 220 Ohm resistors
and build the following circuit:
# import needed modules
import time
import RPi.GPIO as GPIO
GPIO.setmode( GPIO.BCM )
r = 17
g = 27
b = 22
GPIO.setup( r, GPIO.OUT )
GPIO.setup( g, GPIO.OUT )
GPIO.setup( b, GPIO.OUT )
GPIO.output( r, 0 )
GPIO.output( g, 0 )
GPIO.output( b, 0 )
GPIO.output( r, 1 )
time.sleep( 2 )
GPIO.output( r, 0 )
GPIO.output( g, 1 )
time.sleep( 2 )
GPIO.output( g, 0 )
GPIO.output( b, 1 )
time.sleep( 2 )
GPIO.output( b, 0 )
# the program is finished, we put things back in their original state
GPIO.cleanup()
Now with PWM
# import needed modules
import time
import RPi.GPIO as GPIO
GPIO.setmode( GPIO.BCM )
r = 17
g = 27
b = 22
GPIO.setup( r, GPIO.OUT )
GPIO.setup( g, GPIO.OUT )
GPIO.setup( b, GPIO.OUT )
r_pwm = GPIO.PWM( r, 50 )
g_pwm = GPIO.PWM( g, 50 )
b_pwm = GPIO.PWM( b, 50 )
r_pwm.start( 0 )
g_pwm.start( 0 )
b_pwm.start( 0 )
r_pwm.ChangeDutyCycle( 25 )
time.sleep( 0.5 )
r_pwm.ChangeDutyCycle( 50 )
time.sleep( 0.5 )
r_pwm.ChangeDutyCycle( 75 )
time.sleep( 0.5 )
r_pwm.ChangeDutyCycle( 100 )
time.sleep( 0.5 )
r_pwm.ChangeDutyCycle( 0 )
g_pwm.ChangeDutyCycle( 25 )
time.sleep( 0.5 )
g_pwm.ChangeDutyCycle( 50 )
time.sleep( 0.5 )
g_pwm.ChangeDutyCycle( 75 )
time.sleep( 0.5 )
g_pwm.ChangeDutyCycle( 100 )
time.sleep( 0.5 )
g_pwm.ChangeDutyCycle( 0 )
b_pwm.ChangeDutyCycle( 25 )
time.sleep( 0.5 )
b_pwm.ChangeDutyCycle( 50 )
time.sleep( 0.5 )
b_pwm.ChangeDutyCycle( 75 )
time.sleep( 0.5 )
b_pwm.ChangeDutyCycle( 100 )
time.sleep( 0.5 )
b_pwm.ChangeDutyCycle( 0 )
# the program is finished, we put things back in their original state
GPIO.cleanup()
That’s how TVs work, zoom in on LED? zoom in on screen?
Try to get a pink.
Wrapping up Day 1
how to connect at home
careful not to short pins










