Do something real with python

Do something real with python

You've slaved away learning python - but really haven't done anything in the real world? How about controlling a raspberry pi ?

Raspberry Pi can be programmed in almost any language - but most often uses python to control the general purpose input output pins - giving the Raspberry Pi microcomputer the ability to control real things

For example - consider the following. It counts up in binary with LEDs, then reads a button to reset the count. Here's the python code.

# example of programming Raspberry Pi GPIO with gpiozero
# refer to gpiozero.readthedocs.io

import gpiozero as gpzero
from time import sleep

# set up pushbutton and bank of leds
resetbutton = gpzero.Button(3)
leds = gpzero.LEDBoard(26,16,20,21)

# functions to control behavior
def LightsOn ():
    while resetbutton.is_pressed:
        leds.on()
    
def ResetCounter ():
    global counter
    leds.off()
    counter = 0
    
def binary2lights(showThis):
    leds.value = (
        showThis & 0b1000,
        showThis & 0b0100,
        showThis & 0b0010,
        showThis & 0b0001)
    
# setup button handlers    
resetbutton.when_pressed = LightsOn
resetbutton.when_released = ResetCounter

# send 0...15 to lights
while True:
    ResetCounter()
    while counter < 16:
        binary2lights(counter)
        counter += 1
        sleep(1)
    
    

LEDs are the "Hello World" example when the virtual world of programming meets the physical world of you and I. If that's a bit abstract, take a look at this instructional video from LinkedIn Learning...

Interested in more on Raspberry Pi? Please subscribe to this newsletter.

要查看或添加评论,请登录

Mark Niemann-Ross的更多文章

  • Documenting My Code ... For Me

    Documenting My Code ... For Me

    There are two signs of old age: old age, and ..

  • R Meets Hardware

    R Meets Hardware

    R is a programming language for statistical computing and data visualization. It has been adopted in the fields of data…

    2 条评论
  • Party Buzz Kill: modifying data

    Party Buzz Kill: modifying data

    So Steve (SQL), Marsha (C), Bob (Python), and I (R) are at this party. We have TOTALLY cleared the room, especially now…

    2 条评论
  • Rain - Evapotranspiration = mm Water

    Rain - Evapotranspiration = mm Water

    "Eeee-VAP-oooo-TRANS-PURR-ation," I savor the word as I release it into our conversation. I'm still at the party with…

  • Party Buzz Kill: Data Storage

    Party Buzz Kill: Data Storage

    I'm at this party where Bob and Marsha and I are discussing the best languages for programming a Raspberry Pi. Bob…

    5 条评论
  • R Waters My Garden

    R Waters My Garden

    I'm at a party, and the topic of programming languages comes up. A quarter of the room politely leaves, another half…

    10 条评论
  • Caning and Naming

    Caning and Naming

    We've been back from Port Townsend for a week. Progress on the boat isn't as dramatic as it is when we're spending the…

    1 条评论
  • Irrigate with R and Raspberry Pi

    Irrigate with R and Raspberry Pi

    I’m working on my irrigation system. This requires a controller to turn it on and off.

    3 条评论
  • 5 Reasons to Learn Natural Language Processing with R

    5 Reasons to Learn Natural Language Processing with R

    Why learn R? Why learn Natural Language Processing? Here's five reasons..

    1 条评论
  • Performing Natural Language Processing with R

    Performing Natural Language Processing with R

    I recently released a course on Educative covering topics in Natural Language Processing. Different Learners -…

    1 条评论

社区洞察

其他会员也浏览了