Difference between revisions of "Open HW Session 2022"

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
 
(2 intermediate revisions by 2 users not shown)
Line 19: Line 19:
  
 
If you are interested in Sketching With Hardware please come along - no registration required - just join us!
 
If you are interested in Sketching With Hardware please come along - no registration required - just join us!
 +
 +
We have all the hardware for building the system, but please bring your own laptop (with a USB port).
  
 
[[File:Box1.JPG|x300px]]
 
[[File:Box1.JPG|x300px]]
 +
 +
 +
== Script used to kickoff the session ==
 +
 +
‎<syntaxhighlight lang="python" line>
 +
 +
###############################################################
 +
# WS2812 RGB LED Ring Light Breathing
 +
# with the Raspberry Pi Pico Microcontroller
 +
#
 +
# by Joshua Hrisko, Maker Portal LLC (c) 2021
 +
# https://makersportal.com/blog/ws2812-ring-light-with-raspberry-pi-pico
 +
#
 +
# Based on the Example neopixel_ring at:
 +
# https://github.com/raspberrypi/pico-micropython-examples
 +
###############################################################
 +
#
 +
import array, time, utime
 +
from machine import Pin
 +
import rp2
 +
 +
#
 +
############################################
 +
# RP2040 PIO and Pin Configurations
 +
############################################
 +
#
 +
# WS2812 LED Ring Configuration
 +
led_count = 12 # number of LEDs in ring light
 +
PIN_NUM = 13 # pin connected to ring light
 +
brightness = 1.0 # 0.1 = darker, 1.0 = brightest
 +
 +
#
 +
############################################
 +
# Photoresistor declaration
 +
############################################
 +
#
 +
photoresistor_out = Pin(27, Pin.OUT)
 +
photoresistor_out.on()
 +
photoresistor_adc_read = machine.ADC(26)
 +
 +
#
 +
############################################
 +
# HC-SR04 distance sensor declaration
 +
############################################
 +
#
 +
 +
distance_echo = Pin(17, Pin.IN)
 +
distance_trigger = Pin(16, Pin.OUT)
 +
distance = 0
 +
 +
 +
@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT,
 +
            autopull=True, pull_thresh=24) # PIO configuration
 +
 +
# define WS2812 parameters
 +
def ws2812():
 +
    T1 = 2
 +
    T2 = 5
 +
    T3 = 3
 +
    wrap_target()
 +
    label("bitloop")
 +
    out(x, 1)              .side(0)    [T3 - 1]
 +
    jmp(not_x, "do_zero")  .side(1)    [T1 - 1]
 +
    jmp("bitloop")          .side(1)    [T2 - 1]
 +
    label("do_zero")
 +
    nop()                  .side(0)    [T2 - 1]
 +
    wrap()
 +
 +
 +
# Create the StateMachine with the ws2812 program, outputting on pre-defined pin
 +
# at the 8MHz frequency
 +
sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(PIN_NUM))
 +
 +
# Activate the state machine
 +
sm.active(1)
 +
 +
# Range of LEDs stored in an array
 +
ar = array.array("I", [0 for _ in range(led_count)])
 +
#
 +
############################################
 +
# Functions for RGB Coloring
 +
############################################
 +
#
 +
def pixels_show(brightness_input=brightness):
 +
    dimmer_ar = array.array("I", [0 for _ in range(led_count)])
 +
    for ii,cc in enumerate(ar):
 +
        r = int(((cc >> 8) & 0xFF) * brightness_input) # 8-bit red dimmed to brightness
 +
        g = int(((cc >> 16) & 0xFF) * brightness_input) # 8-bit green dimmed to brightness
 +
        b = int((cc & 0xFF) * brightness_input) # 8-bit blue dimmed to brightness
 +
        dimmer_ar[ii] = (g<<16) + (r<<8) + b # 24-bit color dimmed to brightness
 +
    sm.put(dimmer_ar, 8) # update the state machine with new colors
 +
    time.sleep_ms(10)
 +
 +
def pixels_set(i, color):
 +
    ar[i] = (color[1]<<16) + (color[0]<<8) + color[2] # set 24-bit color
 +
       
 +
def breathing_led(color):
 +
    step = 5
 +
    breath_amps = [ii for ii in range(0,255,step)]
 +
    breath_amps.extend([ii for ii in range(255,-1,-step)])
 +
    for ii in breath_amps:
 +
        for jj in range(len(ar)):
 +
            pixels_set(jj, color) # show all colors
 +
        pixels_show(ii/255)
 +
        time.sleep(0.02)
 +
 
 +
#
 +
############################################
 +
# Function for HC-SR04 readout
 +
############################################
 +
#
 +
 +
def ultra():
 +
    distance_trigger.low()
 +
    utime.sleep_us(2)
 +
    distance_trigger.high()
 +
    utime.sleep_us(5)
 +
    distance_trigger.low()
 +
    while distance_echo.value() == 0:
 +
        signaloff = utime.ticks_us()
 +
    while distance_echo.value() == 1:
 +
        signalon = utime.ticks_us()
 +
    timepassed = signalon - signaloff
 +
    global distance
 +
    distance = (timepassed * 0.0343) / 2
 +
    #print("Distance:",distance,"cm")
 +
   
 +
       
 +
#
 +
############################################
 +
# Main Calls and Loops
 +
############################################
 +
#
 +
 +
color = (255,0,0) # looping color
 +
blank = (255,255,255) # color for other pixels
 +
cycles = 25 # number of times to cycle 360-degrees
 +
for ii in range(int(cycles*len(ar))+1):
 +
       
 +
    for jj in range(len(ar)):
 +
        if jj==int(ii%led_count): # in case we go over number of pixels in array
 +
            pixels_set(jj,color) # color and loop a single pixel
 +
        else:
 +
            pixels_set(jj,blank) # turn others off
 +
   
 +
    photoresistor_corrected_readout = photoresistor_adc_read.read_u16() - 60000
 +
    photoresistor_ratio = photoresistor_corrected_readout / 5535.0
 +
   
 +
    brightness = photoresistor_corrected_readout / (5535.0 * (1 / photoresistor_ratio))
 +
    #print(brightness)
 +
   
 +
    pixels_show(brightness) # update pixel colors
 +
    time.sleep(0.05) # wait 50ms
 +
 +
##below: breathing LED
 +
red = (255,0,0)
 +
green = (0,255,0)
 +
blue = (0,0,255)
 +
yellow = (255,255,0)
 +
cyan = (0,255,255)
 +
white = (255,255,255)
 +
blank = (0,0,0)
 +
colors = [blue,yellow,cyan,red,white, green]
 +
 +
 +
 +
while True: # loop indefinitely
 +
   
 +
    ultra()
 +
   
 +
    MAXIMUM_DISTANCE = 120
 +
   
 +
    if distance > MAXIMUM_DISTANCE - 1:
 +
        distance_corrected = MAXIMUM_DISTANCE - 1
 +
    else:
 +
        distance_corrected = distance
 +
       
 +
    range_per_color = MAXIMUM_DISTANCE / len(colors)
 +
   
 +
 +
    breathing_led(colors[(int)(distance_corrected / range_per_color)])
 +
 +
‎</syntaxhighlight>
 +
 +
[[Category:Courses]]

Latest revision as of 14:39, 12 June 2024

Join us for a fun hardware building session[edit]

  • Date: 28.11.2022
  • Time: 17:00 to 22:00
  • Location: Munich, Frauenlobstr. 7A, 3rd Floor
  • no prior knowledge required!

With the open hardware session we would like to get people excited for building their own hardware.

In this session you get a chance to build a system using a Raspberry Pi Pico with sensors (e.g. a distance sensor Ultrasonic Sensor HC-SR04, a light sensor LDR) and actuators (e.g. a speaker Piezo Speaker an a led ring LED Ring NeoPixel).

Can you create a novel and fun game with this hardware? If you don't stay close to the wall the others will hear you. If you don't move around your light will go on. Who is able to sneak to the next room without being caught?

Or do you have a different game idea?

If you are interested in Sketching With Hardware please come along - no registration required - just join us!

We have all the hardware for building the system, but please bring your own laptop (with a USB port).

Box1.JPG


Script used to kickoff the session[edit]

  1 ###############################################################
  2 # WS2812 RGB LED Ring Light Breathing
  3 # with the Raspberry Pi Pico Microcontroller
  4 #
  5 # by Joshua Hrisko, Maker Portal LLC (c) 2021
  6 # https://makersportal.com/blog/ws2812-ring-light-with-raspberry-pi-pico
  7 #
  8 # Based on the Example neopixel_ring at:
  9 # https://github.com/raspberrypi/pico-micropython-examples
 10 ###############################################################
 11 #
 12 import array, time, utime
 13 from machine import Pin
 14 import rp2
 15 
 16 #
 17 ############################################
 18 # RP2040 PIO and Pin Configurations
 19 ############################################
 20 #
 21 # WS2812 LED Ring Configuration
 22 led_count = 12 # number of LEDs in ring light
 23 PIN_NUM = 13 # pin connected to ring light
 24 brightness = 1.0 # 0.1 = darker, 1.0 = brightest
 25 
 26 #
 27 ############################################
 28 # Photoresistor declaration
 29 ############################################
 30 #
 31 photoresistor_out = Pin(27, Pin.OUT)
 32 photoresistor_out.on()
 33 photoresistor_adc_read = machine.ADC(26)
 34 
 35 #
 36 ############################################
 37 # HC-SR04 distance sensor declaration
 38 ############################################
 39 #
 40 
 41 distance_echo = Pin(17, Pin.IN)
 42 distance_trigger = Pin(16, Pin.OUT)
 43 distance = 0
 44 
 45 
 46 @rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT,
 47              autopull=True, pull_thresh=24) # PIO configuration
 48 
 49 # define WS2812 parameters
 50 def ws2812():
 51     T1 = 2
 52     T2 = 5
 53     T3 = 3
 54     wrap_target()
 55     label("bitloop")
 56     out(x, 1)               .side(0)    [T3 - 1]
 57     jmp(not_x, "do_zero")   .side(1)    [T1 - 1]
 58     jmp("bitloop")          .side(1)    [T2 - 1]
 59     label("do_zero")
 60     nop()                   .side(0)    [T2 - 1]
 61     wrap()
 62 
 63 
 64 # Create the StateMachine with the ws2812 program, outputting on pre-defined pin
 65 # at the 8MHz frequency
 66 sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(PIN_NUM))
 67 
 68 # Activate the state machine
 69 sm.active(1)
 70 
 71 # Range of LEDs stored in an array
 72 ar = array.array("I", [0 for _ in range(led_count)])
 73 #
 74 ############################################
 75 # Functions for RGB Coloring
 76 ############################################
 77 #
 78 def pixels_show(brightness_input=brightness):
 79     dimmer_ar = array.array("I", [0 for _ in range(led_count)])
 80     for ii,cc in enumerate(ar):
 81         r = int(((cc >> 8) & 0xFF) * brightness_input) # 8-bit red dimmed to brightness
 82         g = int(((cc >> 16) & 0xFF) * brightness_input) # 8-bit green dimmed to brightness
 83         b = int((cc & 0xFF) * brightness_input) # 8-bit blue dimmed to brightness
 84         dimmer_ar[ii] = (g<<16) + (r<<8) + b # 24-bit color dimmed to brightness
 85     sm.put(dimmer_ar, 8) # update the state machine with new colors
 86     time.sleep_ms(10)
 87 
 88 def pixels_set(i, color):
 89     ar[i] = (color[1]<<16) + (color[0]<<8) + color[2] # set 24-bit color
 90         
 91 def breathing_led(color):
 92     step = 5
 93     breath_amps = [ii for ii in range(0,255,step)]
 94     breath_amps.extend([ii for ii in range(255,-1,-step)])
 95     for ii in breath_amps:
 96         for jj in range(len(ar)):
 97             pixels_set(jj, color) # show all colors
 98         pixels_show(ii/255)
 99         time.sleep(0.02)
100   
101 #
102 ############################################
103 # Function for HC-SR04 readout
104 ############################################
105 #
106 
107 def ultra():
108     distance_trigger.low()
109     utime.sleep_us(2)
110     distance_trigger.high()
111     utime.sleep_us(5)
112     distance_trigger.low()
113     while distance_echo.value() == 0:
114         signaloff = utime.ticks_us()
115     while distance_echo.value() == 1:
116         signalon = utime.ticks_us()
117     timepassed = signalon - signaloff
118     global distance
119     distance = (timepassed * 0.0343) / 2
120     #print("Distance:",distance,"cm")
121     
122         
123 #
124 ############################################
125 # Main Calls and Loops
126 ############################################
127 #
128 
129 color = (255,0,0) # looping color
130 blank = (255,255,255) # color for other pixels
131 cycles = 25 # number of times to cycle 360-degrees
132 for ii in range(int(cycles*len(ar))+1):
133         
134     for jj in range(len(ar)):
135         if jj==int(ii%led_count): # in case we go over number of pixels in array
136             pixels_set(jj,color) # color and loop a single pixel
137         else:
138             pixels_set(jj,blank) # turn others off
139     
140     photoresistor_corrected_readout = photoresistor_adc_read.read_u16() - 60000
141     photoresistor_ratio = photoresistor_corrected_readout / 5535.0
142     
143     brightness = photoresistor_corrected_readout / (5535.0 * (1 / photoresistor_ratio))
144     #print(brightness)
145     
146     pixels_show(brightness) # update pixel colors
147     time.sleep(0.05) # wait 50ms
148 
149 ##below: breathing LED
150 red = (255,0,0)
151 green = (0,255,0)
152 blue = (0,0,255)
153 yellow = (255,255,0)
154 cyan = (0,255,255)
155 white = (255,255,255)
156 blank = (0,0,0)
157 colors = [blue,yellow,cyan,red,white, green]
158 
159 
160 
161 while True: # loop indefinitely
162     
163     ultra()
164     
165     MAXIMUM_DISTANCE = 120
166     
167     if distance > MAXIMUM_DISTANCE - 1:
168         distance_corrected = MAXIMUM_DISTANCE - 1
169     else:
170         distance_corrected = distance
171         
172     range_per_color = MAXIMUM_DISTANCE / len(colors)
173     
174 
175     breathing_led(colors[(int)(distance_corrected / range_per_color)])
176 
177