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. In Hertz, which is simply number of times per second.
Here’s a random function we found on the internet which promises to blink a pin at a given frequency, try to integrate it within your code:
def blink_at_frequency( my_pin, frequency, how_long ): # frequency in Hz, how_long in seconds
for i in range( 0, how_long*frequency ):
time.sleep( (1/frequency)/2 )
GPIO.output( my_pin, 1 )
time.sleep( (1/frequency)/2 )
GPIO.output( my_pin, 0 )
Once it’s integrated, 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?
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
- 1 more 220 Ohm resistor
and build the following circuit:
This is the kind of code that we’re used to:
# 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 )
print( "red" )
GPIO.output( r, 1 )
time.sleep( 2 )
GPIO.output( r, 0 )
print( "green" )
GPIO.output( g, 1 )
time.sleep( 2 )
GPIO.output( g, 0 )
print( "blue" )
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()
Why RGB?
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 )
print( "red" )
print( " 25" )
r_pwm.ChangeDutyCycle( 25 )
time.sleep( 0.5 )
print( " 50" )
r_pwm.ChangeDutyCycle( 50 )
time.sleep( 0.5 )
print( " 75" )
r_pwm.ChangeDutyCycle( 75 )
time.sleep( 0.5 )
print( " 100" )
r_pwm.ChangeDutyCycle( 100 )
time.sleep( 0.5 )
r_pwm.ChangeDutyCycle( 0 )
print( "green" )
print( " 25" )
g_pwm.ChangeDutyCycle( 25 )
time.sleep( 0.5 )
print( " 50" )
g_pwm.ChangeDutyCycle( 50 )
time.sleep( 0.5 )
print( " 75" )
g_pwm.ChangeDutyCycle( 75 )
time.sleep( 0.5 )
print( " 100" )
g_pwm.ChangeDutyCycle( 100 )
time.sleep( 0.5 )
g_pwm.ChangeDutyCycle( 0 )
print( "blue" )
print( " 25" )
b_pwm.ChangeDutyCycle( 25 )
time.sleep( 0.5 )
print( " 50" )
b_pwm.ChangeDutyCycle( 50 )
time.sleep( 0.5 )
print( " 75" )
b_pwm.ChangeDutyCycle( 75 )
time.sleep( 0.5 )
print( " 100" )
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.
Motors
Please grab a 28BYJ-48 stepper motor and, driver board, and 12v plug.
DC motors Vs Stepper motors
Why do you think a drawing machine needs a stepper motor and not a DC motor?
12v & Stepper Wiring

Plug in 12v on the other side of the breadboard !make sure it’s not getting power yet!
Note the linked grounds.
Code
Chasing the magnet
#!/usr/bin/python3
import RPi.GPIO as GPIO
import time
in1 = 6
in2 = 13
in3 = 19
in4 = 26
# careful lowering this, at some point you run into the mechanical limitation of how quick your motor can move
step_sleep = 0.002
# setting up
GPIO.setmode( GPIO.BCM )
GPIO.setup( in1, GPIO.OUT )
GPIO.setup( in2, GPIO.OUT )
GPIO.setup( in3, GPIO.OUT )
GPIO.setup( in4, GPIO.OUT )
# initializing
GPIO.output( in1, 0 )
GPIO.output( in2, 0 )
GPIO.output( in3, 0 )
GPIO.output( in4, 0 )
# the meat
GPIO.output( in1, 1 )
GPIO.output( in2, 0 )
GPIO.output( in3, 0 )
GPIO.output( in4, 0 )
time.sleep( 0.1 )
GPIO.output( in1, 1 )
GPIO.output( in2, 1 )
GPIO.output( in3, 0 )
GPIO.output( in4, 0 )
time.sleep( 0.1 )
GPIO.output( in1, 0 )
GPIO.output( in2, 1 )
GPIO.output( in3, 0 )
GPIO.output( in4, 0 )
time.sleep( 0.1 )
GPIO.output( in1, 0 )
GPIO.output( in2, 1 )
GPIO.output( in3, 1 )
GPIO.output( in4, 0 )
time.sleep( 0.1 )
GPIO.output( in1, 0 )
GPIO.output( in2, 0 )
GPIO.output( in3, 1 )
GPIO.output( in4, 0 )
time.sleep( 0.1 )
GPIO.output( in1, 0 )
GPIO.output( in2, 0 )
GPIO.output( in3, 1 )
GPIO.output( in4, 1 )
time.sleep( 0.1 )
GPIO.output( in1, 0 )
GPIO.output( in2, 0 )
GPIO.output( in3, 0 )
GPIO.output( in4, 1 )
time.sleep( 0.1 )
GPIO.output( in1, 1 )
GPIO.output( in2, 0 )
GPIO.output( in3, 0 )
GPIO.output( in4, 1 )
time.sleep( 0.1 )
# all done, reset & cleanup
GPIO.output( in1, 0 )
GPIO.output( in2, 0 )
GPIO.output( in3, 0 )
GPIO.output( in4, 0 )
GPIO.cleanup()
Note how the LEDs are following the code.
Tiny steps! Very accurate.













