Raspberry Pi LESSON 5 HOMEWORK SOLUTION: Understanding Binary Numbers

preview_player
Показать описание
Guys the pinout I show in this video has an error on Tx/Rx. You can download your own correct Pinout from my WEB site HERE:

Announcing the Most Awesome Raspberry Pi Lessons of All Times! This time we RUMBLE!

In this class series, we will be using the most excellent Sunfounder Ultimate Raspberry Pi kit, available here: (Affiliate Links)

The box of straight jumper wires to keep your build neat and tidy are available here:

In this lesson I will show how to build a 5 bit binary counter. We do this with the Raspberry Pi, and 5 LED. The LED blink in Binary count order between 0 and 31.

You guys get your hardware ordered so you can follow along at home!

You will also need a Raspberry Pi. I suggest the Raspberry Pi 4. If you do not already have one, this is the most suitable gear I could find:

The Raspberry Pi's are sort of pricy right now, so you can look on ebay or elsewhere to see if there are any deals. You will need a SD card. If you do not already have one, this is a good one:

I like using a wireless keyboard and mouse to have fewer wires. You can certainly use your USB keyboard and mouse, but if you want a nice wireless one, this one works on the pi.

You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:

#BINARY
#RESISTTHEMETAVERSE
#RASPBERRYPI
Рекомендации по теме
Комментарии
Автор

There are 10 types of people in this world:
Those who understand binary, and those who don't.

aaroncarman
Автор

In the past, I would have counted from 0 to 31 in a for-loop, and then decoded the decimal number to binary. For this exercise, I created an array, to store the 5 '1's and '0's, and then did the math in binary, by starting the loop through with toggling bit[1] from '0' to '1', and then when toggling from '1' to '0', setting a 'Carry' flag. For bit[2] to [5], using the Carry flag to indicate the need to set, or reset the bit, turning the LED On, (or Off) and managing the Carry flag, as necessary. Your way was simpler. Many thanks for this new series.

keyboardlearning
Автор

Ok took me awhile to get it right but your method was much simpler for us just starting to write code then some of the solutions that others had entered on Youtube. I am having fun doing this so it is keeping me busy at 73 years of age

WilliamLundy-et
Автор

Thanks, Paul.
Jolly good lesson!! I would never thought of putting the resistors going down across the gap and I will also be getting some of those short wires.

charlotteswift
Автор

Always loving the breadboard wandering ants!

kyriakospapadopoulos
Автор

Me and my friend Eshhu are your biggest fans!! We Watch all ur arduino videos and happily on video 52! Keep up the good work!!!

ryanrangel
Автор

I am really enjoying your lessons and am learning so much. Here's my code for this lesson:

# Blink 5 LED in Binary counting from 0 to 31
import RPi.GPIO as GPIO
from time import sleep

# Setup GPIO Pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)

# Initialize setup
a = 0
b = 0
c = 0
d = 0
e = 0
temp = 0

# Create a function to convert from Base10 to Base2
def Convert_base10_base2(i):
a = i % 2
temp = i // 2
b = temp % 2
temp = temp // 2
c = temp % 2
temp = temp // 2
d = temp % 2
temp = temp // 2
e = temp % 2
return a, b, c, d, e

# Light up each binary number
for i in range(0, 32, 1):
a, b, c, d, e = Convert_base10_base2(i)
GPIO.output(5, a), GPIO.output(6, b), GPIO.output(13, c), GPIO.output(19, d), GPIO.output(26, e)
sleep(1)
GPIO.cleanup()

TuckerSidhu
Автор

Yeah! Raspberry Pi finally arrived so I can start the lessons!
In the meantime I completed the Paul McWhorter Python lessons and did a considerable
amount of additional Python learning.

Here is my take on the binary counter;
import RPi.GPIO as PIN # 'PIN' can be named anything you like
# PIN.setmode(PIN.BOARD) # use for board pin numbering scheme
PIN.setmode(PIN.BCM) # use for BCM pin numbering scheme

from time import sleep

# Set GPIO pin numbers
led1 = 4
led2 = 17
led3 = 27
led4 = 22
led5 = 26

# Setup pins as outputs
PIN.setup(led1, PIN.OUT)
PIN.setup(led2, PIN.OUT)
PIN.setup(led3, PIN.OUT)
PIN.setup(led4, PIN.OUT)
PIN.setup(led5, PIN.OUT)

# Send command to all pins to turn them off, '0' does this
PIN.output(led1, 0)
PIN.output(led2, 0)
PIN.output(led3, 0)
PIN.output(led4, 0)
PIN.output(led5, 0)

# Create for loop to count. Create a list that formats the count
# and outputs to a binary Python list. example [0, 1, 1, 0, 1] is 13
for i in range(0, 32):
lst = [int(i) for i in format(i, '07b') [2:]]
print(str(i) + " to binary: " + str(lst))
sleep(.5)

# Use the list and list indexing to assign 0 or 1 value to led output pin
PIN.output(led1, lst[4])
PIN.output(led2, lst[3])
PIN.output(led3, lst[2])
PIN.output(led4, lst[1])
PIN.output(led5, lst[0])

sleep(5)

PIN.cleanup()

TheScissorunner
Автор

Haha, yet another ant on your breadboard! at: 40:24 !! It stays on the board until 42:22
Thanks Paul for another lesson!

danielsaenz
Автор

I am legend!
I did this project back in the "New Arduino Tutorials, so I decided to make the assignment a little more complex by using if and elif statements to turn the LEDs on and on.
1. The Code uses if-statements to first checks to see if the 1splace LED is on or off (x1). It then determines if a one will be carried over to the next place.
2. The second set of if-statements checks to see if a one was carried over from the 1splace. Depending on the answer, the program will turn or turn off the x2 LED and determine if a 1 is carried over to the 4splace.
3. Step 2 is repeated for all the LEDs.
4. The final if-statement determines if a one is carried from the 16splace to the 32splace.
4A. If the answer is now, the code starts back at step 1.
4B. If the answer is yes, their is now more places to dump in the 1, so the program exits the while loop.
Due to the number of necessary if-statements and variable required, it probably took as long to write this code as Mr. McWhorter's solution, but it was good practice.
If I had time to go back and do it, I would create a function. It would make the code so much cleaner
Great lessons as always!

jonathanlanders
Автор

Here is my code for the lesson 5 assignment. I retired as a programmer in 1998, but I'm rather new to Python and Linux. I wrote a program that I think handles a variable number of bits. The GPIO assignments make that not entirely true. I had to do a lot of Googling to solve the problem. I know I've gone beyond the scope of the lesson, but I couldn't get a good nights sleep until I solved the problem. I also had a HW problem. I discovered a resistor measured much higher than it should be. The corresponding LED was very dim. The resistor was not from my SunFounder kit, but from an old Arduino kit.

# Binary counter with LEDs
# Works with variable number of bits, except for GPIO assignments

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

GPIO.setup(29, GPIO.OUT)
GPIO.setup(31, GPIO.OUT)
GPIO.setup(33, GPIO.OUT)
GPIO.setup(35, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)

On = True
Off = False

import time
OnTime = 1
OffTime = .25

i = 0 #Number counter
j = 0 #Bit index
num_bits = 5
max_binary_values = 2**num_bits

GPIO_Num = (37, 35, 33, 31, 29) #LED GPIO pin numbers - Reverse pin numbers if needed
LED_Status = []
LED_Status = [Off for i in range(number of bits)] #Set Array size

i = 0
while i < max_binary_values:
b = bin(i)[2:] #Convert Number to string of ones and zeros, ignore leading 0b
b = b.zfill(num_bits) #Pad String with zeros if less than number of bits
j = 0
while j < num_bits: #Turn off all LEDs
GPIO.output(GPIO_Num[j], Off) #turn LED Off# print(j)
j = j + 1
time.sleep(OffTime)
j = 0
while j < num_bits: #Set LED_Status Array for current binary number
binarydigit = int(b[j])
if binarydigit == 0:
LED_Status[j] = Off
if binarydigit == 1:
LED_Status[j] = On
j = j + 1
j = 0
while j < num_bits: #turn on LED Pattern
GPIO.output(GPIO_Num[j], LED_Status[j])
j = j + 1
time.sleep(OnTime)
i = i + 1
GPIO.cleanup()

WilliamBurlingame
Автор

I did achieve legendary status but I used your instruction here to improve my circuit layout. I'm using the RP400 so it gets a bit claustrophobic building out from the back of the keyboard. I'm going to invest in a breakout cable to make it a bit easier moving forward.

RobVollmar
Автор

Well I guess I folded, but not quite like a cheap WalMart lawn chair. I was trying to use some sort of looping code with a counter, but folded before watching your solution. But after you defined the 0 - 15 count, why not just copy and paste those 16, then all you would have had to edit would have been turning on LED5 on the second block of 16?

codecage
Автор

I'm glad there were only 5 red LEDs in the kit...

peterkarlsson
Автор

The rain and storm had no effect on your quality of teaching...hats off!

michealmyers
Автор

I am legend. I did not use any jumper wires and instead plugged all the LEDs into the top ground rail. I did use the same GPIO pins but in the reverse order. My code is much more compact.
for num in range (32):
bNum = format (num, '05b')
GPIO.output(29, int(bNum[0]))
GPIO.output(31, int(bNum[1]))
GPIO.output(33, int(bNum[2]))
GPIO.output(35, int(bNum[3]))
GPIO.output(37, int(bNum[4]))
time.sleep(1)

brucemilyko
Автор

35:38 is it only me who saw a ant? Thought it was on my screen lol

mystery_
Автор

Hi Paul, great series, thank you! Is the source code for your solutions somewhere available for download? I’d like to keep your code next to mine for comparison.

bhnienhuis
Автор

Paul - my 8yo son and I are learning RPi together from your excellent videos. Thank you for the taking the time to create this content! Here is our code, would love feedback if any:

import RPi.GPIO as gpio
gpio.setmode(gpio.BOARD)
gpio.setup(11, gpio.OUT)
gpio.setup(13, gpio.OUT)
gpio.setup(15, gpio.OUT)
gpio.setup(29, gpio.OUT)
gpio.setup(31, gpio.OUT)
import time

p1, p2, p3, p4, p5 = 0, 0, 0, 0, 0
binary = [p5, p4, p3, p2, p1]

for p5 in range(0, 2, 1):
binary = [p5, p4, p3, p2, p1]
for p4 in range(0, 2, 1):
binary = [p5, p4, p3, p2, p1]
for p3 in range(0, 2, 1):
binary = [p5, p4, p3, p2, p1]
for p2 in range(0, 2, 1):
binary = [p5, p4, p3, p2, p1]
for p1 in range(0, 2, 1):
binary = [p5, p4, p3, p2, p1]
print(binary)
time.sleep(1)
gpio.output(11, p1)
gpio.output(13, p2)
gpio.output(15, p3)
gpio.output(29, p4)
gpio.output(31, p5)
time.sleep(1)


gpio.output(11, 0)
gpio.output(13, 0)
gpio.output(15, 0)
gpio.output(29, 0)
gpio.output(31, 0)
gpio.cleanup()

PrabudhGoel-xi
Автор

Thanks Paul for the lesson and the homework assignment / solution vids! I'm really appreciating your teaching style and your effort in doing this for us! I do have one question... Which brand/model sketchpad and software are you using with the cool engineering graph background? I'd love to have one just like it!

Lehibob