black and lint metrox circuitpython code

This commit is contained in:
brentru 2021-01-29 09:32:13 -05:00
parent 48178b509c
commit 3a9955e6e6
33 changed files with 1161 additions and 0 deletions

View file

@ -0,0 +1,22 @@
"""
'audio.py'.
=================================================
alarm circuit with a FSR and a speaker
requires:
- CircuitPython_CharLCD Module
"""
import audioio
import analogio
import board
f = open("siren.wav", "rb")
a = audioio.AudioOut(board.A0, f)
fsr = analogio.AnalogIn(board.A2)
threshold = 200
while True:
if fsr.value < threshold:
a.play(loop=True)
else:
a.pause()

View file

@ -0,0 +1,23 @@
"""
'blinking_led.py'.
=================================================
blinks a led using digitalio
"""
import time
import digitalio
import board
led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()
while True:
# turn led ON
led.value = True
# wait 1 second
time.sleep(1.0)
# turn led off
led.value = False
# wait 1 second
time.sleep(1.0)

View file

@ -0,0 +1,18 @@
"""
'button_press.py'.
=================================================
push the button and light up a led
"""
import digitalio
import board
led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()
button = digitalio.DigitalInOut(board.D2)
button.switch_to_input()
while True:
btn_state = button.value
led.value = not btn_state

View file

@ -0,0 +1,62 @@
"""
'char_lcd.py'
=================================================
hello world using 16x2 character lcd
requires:
- CircuitPython_CharLCD Module
"""
import time
import digitalio
import adafruit_character_lcd
from board import D7, D8, D9, D10, D11, D12, D13
# Character LCD Config:
# modify this if you have a different sized charlcd
lcd_columns = 16
lcd_rows = 2
# Metro Express Pin Config:
lcd_rs = digitalio.DigitalInOut(D7)
lcd_en = digitalio.DigitalInOut(D8)
lcd_d7 = digitalio.DigitalInOut(D12)
lcd_d6 = digitalio.DigitalInOut(D11)
lcd_d5 = digitalio.DigitalInOut(D10)
lcd_d4 = digitalio.DigitalInOut(D9)
lcd_backlight = digitalio.DigitalInOut(D13)
# Init the lcd class
lcd = adafruit_character_lcd.Character_LCD(
lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight
)
# Print a 2x line message
lcd.message("hello\ncircuitpython")
# Wait 5s
time.sleep(5)
# Demo showing cursor
lcd.clear()
lcd.show_cursor(True)
lcd.message("showing cursor ")
# Wait 5s
time.sleep(5)
# Demo showing the blinking cursor
lcd.clear()
lcd.blink(True)
lcd.message("Blinky Cursor!")
# Wait 5s
time.sleep(5)
lcd.blink(False)
# Demo scrolling message LEFT
lcd.clear()
scroll_msg = "Scroll"
lcd.message(scroll_msg)
# Scroll to the left
for i in range(lcd_columns - len(scroll_msg)):
time.sleep(0.5)
lcd.move_left()
# Demo turning backlight off
lcd.clear()
lcd.message("going to sleep\ncya later!")
lcd.set_backlight(False)
time.sleep(2)

View file

@ -0,0 +1,40 @@
"""
'colorful_light.py'.
=================================================
RGB LED control with circuitpython
"""
import board
import digitalio
RED = [True, False, False]
GREEN = [False, True, False]
BLUE = [False, False, True]
YELLOW = [True, True, False]
CYAN = [False, True, True]
MAGENTA = [True, False, True]
WHITE = [True, True, True]
BLACK = [False, False, False]
color_array = [RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK]
red_led = digitalio.DigitalInOut(board.D9)
green_led = digitalio.DigitalInOut(board.D10)
blue_led = digitalio.DigitalInOut(board.D11)
rgb_led = [red_led, green_led, blue_led]
for led in rgb_led:
led.switch_to_output()
def set_color(color):
"""sets the rgb led's cathode value."""
rgb_led[0].value = not color[0]
rgb_led[1].value = not color[1]
rgb_led[2].value = not color[2]
while True:
set_color(GREEN)

View file

@ -0,0 +1,120 @@
"""
'eight_leds.py'.
=================================================
lights up 8 leds with different animations
"""
import time
import board
import digitalio
delay_time = 0.1
led_pins = [
digitalio.DigitalInOut(board.D2),
digitalio.DigitalInOut(board.D3),
digitalio.DigitalInOut(board.D4),
digitalio.DigitalInOut(board.D5),
digitalio.DigitalInOut(board.D6),
digitalio.DigitalInOut(board.D7),
digitalio.DigitalInOut(board.D8),
digitalio.DigitalInOut(board.D9),
]
for pin in led_pins:
pin.switch_to_output()
def one_after_another_no_loop():
"""turns one LED on at a time, not looping at the end."""
led_pins[0].value = True
time.sleep(delay_time)
led_pins[1].value = True
time.sleep(delay_time)
led_pins[2].value = True
time.sleep(delay_time)
led_pins[3].value = True
time.sleep(delay_time)
led_pins[4].value = True
time.sleep(delay_time)
led_pins[5].value = True
time.sleep(delay_time)
led_pins[6].value = True
time.sleep(delay_time)
led_pins[7].value = True
time.sleep(delay_time)
led_pins[7].value = False
time.sleep(delay_time)
led_pins[6].value = False
time.sleep(delay_time)
led_pins[5].value = False
time.sleep(delay_time)
led_pins[4].value = False
time.sleep(delay_time)
led_pins[3].value = False
time.sleep(delay_time)
led_pins[2].value = False
time.sleep(delay_time)
led_pins[1].value = False
time.sleep(delay_time)
led_pins[0].value = False
time.sleep(delay_time)
def one_after_another_loop():
"""turns one LED on at a time, looping at the end."""
for led in led_pins:
led.value = True
time.sleep(delay_time)
for led in led_pins[::-1]:
led.value = False
time.sleep(delay_time)
def one_on_at_a_time():
"""turns one LED on at a time, looping at the end."""
led_array_length = len(led_pins)
for i in range(10 * led_array_length):
j = i % led_array_length
led_pins[j].value = True
time.sleep(delay_time)
led_pins[j].value = False
def in_and_out():
"""fades the LEDs in and out."""
for i in range(3):
off_led = i - 1
if i == 0:
off_led = 3
on_led_1 = 3 - i
on_led_2 = 4 + i
off_led1 = 3 - off_led
off_led2 = 4 + off_led
for pin_range in led_pins:
for _ in range(10):
pin_range.value = True
time.sleep(delay_time)
pin_range.value = False
for i in range(3):
i = 3 - i
off_led = i + 1
if i == 3:
off_led = 0
on_led_1 = 3 - i
on_led_2 = 4 + i
off_led1 = 3 - off_led
off_led2 = 4 + off_led
led_pins[on_led_1].value = True
led_pins[on_led_2].value = True
led_pins[off_led1].value = False
led_pins[off_led2].value = False
time.sleep(delay_time)
while True:
one_after_another_no_loop()
# one_after_another_loop()
# one_on_at_a_time()
# in_and_out()

View file

@ -0,0 +1,88 @@
"""
'eight_more_leds.py'.
=================================================
LED light show with a 74HC595 shift register
"""
import time
import board
import digitalio
DATA = digitalio.DigitalInOut(board.D2)
DATA.switch_to_output()
CLK = digitalio.DigitalInOut(board.D3)
CLK.switch_to_output()
LATCH = digitalio.DigitalInOut(board.D4)
LATCH.switch_to_output()
BITS = [
0b00000001,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
]
MASKS = [
0b11111110,
0b11111101,
0b11111011,
0b11110111,
0b11101111,
0b11011111,
0b10111111,
0b01111111,
]
def update_leds(leds):
"""updates the LED state."""
LATCH.value = False
for bit in range(8):
if leds & (1 << bit):
DATA.value = True
else:
DATA.value = False
CLK.value = True
CLK.value = False
LATCH.value = True
def update_leds_long(value):
"""uses bitmasking to update LEDs."""
LATCH.value = False
val = value
# repeat once for each bit
for _ in range(8):
# use bitmask to select only the eighth bit in number
bit = val & 0b10000000
val <<= 1
if bit == 128:
DATA.value = True
else:
DATA.value = False
CLK.value = True
time.sleep(0.01)
CLK.value = False
LATCH.value = True
def change_led(led, state, led_state):
"""changes the LED's state."""
led_state = led_state & MASKS[led]
if state:
led_state = led_state | BITS[led]
# print(led_state)
update_leds(led_state)
LED_STATE = 0
while True:
change_led(3, True, LED_STATE)
for j in range(0, 256, 1):
update_leds(j)
time.sleep(0.4)

View file

@ -0,0 +1,18 @@
"""
'fading_led.py'.
=================================================
fades a LED using pulseio's PWM
"""
import pulseio
import board
led = pulseio.PWMOut(board.D13, frequency=500, duty_cycle=0)
while True:
for i in range(100):
if i < 50: # fade up
led.duty_cycle = int(i * 2 * 65535 / 100)
else: # fade down
led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100)

View file

@ -0,0 +1,75 @@
"""
'ir_neopixel.py'.
=================================================
control a NeoPixel using an (NEC) IR Remote
requires:
- adafruit_irremote library
- neopixel library
"""
import board
import pulseio
import neopixel
import adafruit_irremote
RED = [True, False, False]
GREEN = [False, True, False]
BLUE = [False, False, True]
YELLOW = [True, True, False]
CYAN = [False, True, True]
MAGENTA = [True, False, True]
WHITE = [True, True, True]
BLACK = [False, False, False]
metro_neopixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pulsein = pulseio.PulseIn(board.D6, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
# size must match what you are decoding! for NEC use 4
received_code = bytearray(4)
last_code = None
def set_neopixel_color(color):
metro_neopixel[0] = color
metro_neopixel.show()
while True:
try:
pulses = decoder.read_pulses(pulsein)
except MemoryError as e:
print("Memory Error Occured: ", e)
continue
try:
code = decoder.decode_bits(pulses, debug=False)
except adafruit_irremote.IRNECRepeatException:
print("NEC Code Repeated, doing last command")
code = last_code
except adafruit_irremote.IRDecodeException as e:
print("Failed to decode: ", e)
except MemoryError as e:
print("Memory Error Occured: ", e)
print(code[2])
if code[2] == 247:
set_neopixel_color(RED)
elif code[2] == 119:
set_neopixel_color(GREEN)
elif code[2] == 183:
set_neopixel_color(BLUE)
elif code[2] == 215:
set_neopixel_color(YELLOW)
elif code[2] == 87:
set_neopixel_color(CYAN)
elif code[2] == 151:
set_neopixel_color(MAGENTA)
elif code[2] == 231:
set_neopixel_color(WHITE)
elif code[2] == 215:
set_neopixel_color(BLACK)
else:
set_neopixel_color(BLACK)
last_code = code

View file

@ -0,0 +1,47 @@
"""
'ir_sensor.py'.
=================================================
control a LED with an IR Remote
requires:
- adafruit_irremote library
"""
import adafruit_irremote
import board
import digitalio
import pulseio
led = digitalio.DigitalInOut(board.D11)
led.switch_to_output()
pulsein = pulseio.PulseIn(board.D6, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
# size must match what you are decoding! for NEC use 4
received_code = bytearray(4)
last_code = None
while True:
try:
pulses = decoder.read_pulses(pulsein)
except MemoryError as e:
print("Memory Error Occured: ", e)
continue
try:
code = decoder.decode_bits(pulses, debug=False)
except adafruit_irremote.IRNECRepeatException:
print("NEC Code Repeated, doing last command")
code = last_code
except adafruit_irremote.IRDecodeException as e:
print("Failed to decode: ", e)
except MemoryError as e:
print("Memory Error Occured: ", e)
print(code[2])
if code[2] == 247:
led.value = True
else:
led.value = False
last_code = code

View file

@ -0,0 +1,23 @@
"""
'button_press_on_off.py'.
=================================================
lightswitch-like operation with two buttons and a led
"""
import board
import digitalio
led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()
btn1 = digitalio.DigitalInOut(board.D2)
btn1.switch_to_input()
btn2 = digitalio.DigitalInOut(board.D3)
btn2.switch_to_input()
while True:
if not btn1.value:
led.value = False
elif not btn2.value:
led.value = True

View file

@ -0,0 +1,30 @@
"""
'mib_button_press_pwm.py'.
=================================================
fade a led in and out using two buttons
"""
import time
import digitalio
import board
import pulseio
led = pulseio.PWMOut(board.D13)
btn1 = digitalio.DigitalInOut(board.D3)
btn2 = digitalio.DigitalInOut(board.D2)
btn1.switch_to_input()
btn2.switch_to_input()
while True:
BRIGHTNESS = led.duty_cycle
# If button
if not btn1.value:
BRIGHTNESS += 100
if not btn2.value:
BRIGHTNESS -= 100
BRIGHTNESS = max(0, BRIGHTNESS)
BRIGHTNESS = min(44000, BRIGHTNESS)
led.duty_cycle = BRIGHTNESS
time.sleep(0.001)

View file

@ -0,0 +1,43 @@
"""
'mib_char_lcd_light.py'
=================================================
light sensor circuit. displays output on charlcd
requires:
- CircuitPython_CharLCD Module
"""
import time
import analogio
import digitalio
import adafruit_character_lcd
from board import D7, D8, D9, D10, D11, D12, D13, A0
# Character LCD Config:
# modify this if you have a different sized charlcd
lcd_columns = 16
lcd_rows = 2
# Metro Express Pin Config:
lcd_rs = digitalio.DigitalInOut(D7)
lcd_en = digitalio.DigitalInOut(D8)
lcd_d7 = digitalio.DigitalInOut(D12)
lcd_d6 = digitalio.DigitalInOut(D11)
lcd_d5 = digitalio.DigitalInOut(D10)
lcd_d4 = digitalio.DigitalInOut(D9)
lcd_backlight = digitalio.DigitalInOut(D13)
light = analogio.AnalogIn(A0)
# Init the lcd class
lcd = adafruit_character_lcd.Character_LCD(
lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight
)
while True:
lcd.clear()
percent = str(100 - ((light.value / 65535) * 100))
nice = percent[: percent.find(".")]
lcd.message(nice + "% bright")
lcd.message(str(light.value))
time.sleep(1)
# increment our elapsed_secs variable each time a second passes

View file

@ -0,0 +1,52 @@
"""
'mib_colorful_light.py'.
=================================================
random RGB LED color selection with pulseio
requires:
-simpleio
"""
import time
import random
import board
import pulseio
from simpleio import map_range
RED = [100, 0, 0]
ORANGE = [50, 5, 0]
YELLOW = [100, 100, 0]
GREEN = [0, 100, 0]
TEAL = [0, 50, 5]
CYAN = [0, 100, 100]
BLUE = [0, 0, 100]
MAGENTA = [100, 0, 100]
WHITE = [100, 100, 100]
BLACK = [0, 0, 0]
color_array = [RED, ORANGE, YELLOW, GREEN, TEAL, BLUE, CYAN, MAGENTA, WHITE, BLACK]
red_led = pulseio.PWMOut(board.D9)
green_led = pulseio.PWMOut(board.D10)
blue_led = pulseio.PWMOut(board.D11)
rgb_led_array = [red_led, green_led, blue_led]
def set_color(color):
"""sets the rgb led's cathodes."""
print("Setting (%0.2f, %0.2f, %0.2f)" % (color[0], color[1], color[2]))
rgb_led_array[0].duty_cycle = int(map_range(color[0], 0, 100, 65535, 0))
rgb_led_array[1].duty_cycle = int(map_range(color[1], 0, 100, 65535, 0))
rgb_led_array[2].duty_cycle = int(map_range(color[2], 0, 100, 65535, 0))
def random_color():
"""generates a random color."""
rnd_color = random.randrange(len(color_array))
set_color(color_array[rnd_color])
while True:
random_color()
time.sleep(2)

View file

@ -0,0 +1,41 @@
"""
'mib_motor.py'.
=================================================
spins a DC motor using pulseio
"""
import time
import board
import pulseio
motor_pin = board.D9
motor = pulseio.PWMOut(motor_pin, frequency=1000)
def motor_on_then_off_with_speed():
"""turns the motor on, then off, using PWM."""
on_speed = 0.80
on_time = 2.5
off_speed = 0.10
off_time = 1.0
motor.duty_cycle = int(on_speed * 65535)
time.sleep(on_time)
motor.duty_cycle = int(off_speed * 65535)
time.sleep(off_time)
def motor_acceleration():
"""accelerates the motor forwards and backwards."""
delay_time = 0.05
for speed in range(0, 100, 1):
motor.duty_cycle = int(speed / 100 * 65535)
time.sleep(delay_time)
for speed in range(100, 0, -1):
motor.duty_cycle = int(speed / 100 * 65535)
time.sleep(delay_time)
while True:
motor_on_then_off_with_speed()
# motor_acceleration()

View file

@ -0,0 +1,23 @@
"""
'mib_photo_sensor_night_light_sensor.py'.
=================================================
turns off and on a led using a photo sensor
"""
import analogio
import board
import digitalio
threshold_value = 60000
led = digitalio.DigitalInOut(board.D9)
led.switch_to_output()
light_sensor = analogio.AnalogIn(board.A0)
while True:
if light_sensor.value > threshold_value:
led.value = True
else:
led.value = False

View file

@ -0,0 +1,16 @@
"""
'mib_potentiometer_pwm.py'.
=================================================
fades a led using a potentiometer
"""
import analogio
import board
import pulseio
led = pulseio.PWMOut(board.D9)
pot = analogio.AnalogIn(board.A0)
while True:
led.duty_cycle = pot.value

View file

@ -0,0 +1,21 @@
"""
'mib_potentiometer_THRESHOLD.py'.
=================================================
turns on a LED when the potentiometer is above a half-turn
"""
import analogio
import board
import digitalio
LED = digitalio.DigitalInOut(board.D13)
LED.switch_to_output()
POT = analogio.AnalogIn(board.A0)
THRESHOLD = 512
while True:
if POT.value > THRESHOLD:
LED.value = True
else:
LED.value = False

View file

@ -0,0 +1,39 @@
"""
'mib_servo.py'.
=================================================
sweeping a servo with an analog potentiometer
requires:
- Adafruit_CircuitPython_Motor
"""
import time
import analogio
import board
import pulseio
from adafruit_motor import servo
SERVO = servo.Servo(pulseio.PWMOut(board.D9))
POTE = analogio.AnalogIn(board.A0)
def servo_sweep():
"""sweeps the servo."""
for angle_fwd in range(0, 180, 1):
SERVO.angle = angle_fwd
time.sleep(0.01)
for angle_bkwd in range(180, 0, -1):
SERVO.angle = angle_bkwd
time.sleep(0.01)
def pote_sweep():
"""assigns servo value to an analog potentiometer value."""
val = POTE.value / 65536
SERVO.angle = 180 * val
time.sleep(0.05)
while True:
servo_sweep()
# pote_sweep()

28
MetroX_CircuitPython/motor.py Executable file
View file

@ -0,0 +1,28 @@
"""
'MOTOR.py'.
=================================================
spin a DC MOTOR using digitalio
"""
import time
import board
import digitalio
MOTOR_PIN = board.D9
MOTOR = digitalio.DigitalInOut(MOTOR_PIN)
MOTOR.switch_to_output()
def motor_on_then_off():
"""toggles the motor."""
on_time = 2.5
off_time = 1.0
MOTOR.value = True
time.sleep(on_time)
MOTOR.value = False
time.sleep(off_time)
while True:
motor_on_then_off()

View file

@ -0,0 +1,38 @@
"""
'neopixel_builtin.py'.
=================================================
playing with the metro express builtin NeoPixel
"""
import time
import board
import neopixel
# we only have one neopixel on our metro express
NEO = neopixel.NeoPixel(board.NEOPIXEL, 1)
NEO.brightness = 0.5
# primary colors
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
COLORS = [RED, GREEN, BLUE]
for i in COLORS:
NEO[0] = i
NEO.show()
time.sleep(0.3)
# shows all colors by trying all combinations of RGB levels
for red_level in range(0, 256, 100):
for green_level in range(0, 256, 100):
for blue_level in range(0, 256, 100):
print("red: ", red_level, "green: ", green_level, "blue: ", blue_level)
NEO[0] = (red_level, green_level, blue_level)
NEO.show()
# wait 0.2 seconds to give us time to see the colors
time.sleep(1)
NEO[0] = (0, 0, 0)
NEO.show()

View file

@ -0,0 +1,18 @@
"""
'photo_sensor.py'.
=================================================
uses LIGHT to control a LED
"""
import analogio
import board
import pulseio
from simpleio import map_range
LED = pulseio.PWMOut(board.D9)
LIGHT = analogio.AnalogIn(board.A0)
while True:
LIGHT_VAL = map_range(LIGHT.value, 20000, 32766, 0, 32766)
LED.duty_cycle = int(LIGHT_VAL)

View file

@ -0,0 +1,28 @@
"""
'piezo_music.py'.
=================================================
Twinkle Twinkle with a piezo!
requires:
- simpleio library
"""
import time
import board
from simpleio import tone
NOTES = "ccdcfeccdcgf "
BEATS = [1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 4]
TONES = {"c": 263, "d": 293, "e": 329, "f": 349, "g": 391, "a": 440, "b": 493, "C": 523}
TEMPO = 300
# play the notes!
for i, item in enumerate(NOTES):
tempodelay = 60 / TEMPO
note = NOTES[i]
beat = BEATS[i]
print(note, end="")
if note == " ":
time.sleep(beat * tempodelay)
else:
tone(board.D9, TONES[note], beat * tempodelay)
time.sleep(tempodelay / 2)

View file

@ -0,0 +1,25 @@
"""
'potentiometer.py'.
=================================================
control a LED's brightness using a potentiometer
"""
import time
import digitalio
import analogio
import board
LED = digitalio.DigitalInOut(board.D13)
LED.switch_to_output()
POT = analogio.AnalogIn(board.A0)
SENSOR_VAL = 0
while True:
# potentiometer value/max potentiometer value
SENSOR_VAL = POT.value / 65536
print(SENSOR_VAL)
LED.value = True
time.sleep(SENSOR_VAL)
LED.value = False
time.sleep(SENSOR_VAL)

View file

@ -0,0 +1,21 @@
"""
'potentiometer_plotter.py'.
=================================================
using Mu-Editor v1.0.0b15+ to plot potentiometer values
"""
import time
import analogio
import board
from simpleio import map_range
pote = analogio.AnalogIn(board.A0)
sensor_val = 0
while True:
sensor_val = pote.value
sensor_val = map_range(sensor_val, 0, 65296, -1000, 1000)
tup = (sensor_val,)
print(tup)
time.sleep(1)

17
MetroX_CircuitPython/relay.py Executable file
View file

@ -0,0 +1,17 @@
"""
'relay.py'.
=================================================
drives a small relay
"""
import time
import board
import digitalio
RELAY = digitalio.DigitalInOut(board.D2)
RELAY.switch_to_output()
while True:
RELAY.value = not RELAY.value
time.sleep(1)

20
MetroX_CircuitPython/servo.py Executable file
View file

@ -0,0 +1,20 @@
"""
'servo.py'.
=================================================
move a hobby servo (towerpro sg92r) w/ 3-wire interface
requires:
- Adafruit_CircuitPython_Motor
"""
import time
import board
import pulseio
from adafruit_motor import servo
SERVO = servo.Servo(pulseio.PWMOut(board.D9))
while True:
SERVO.angle = 0
time.sleep(2)
SERVO.angle = 90
time.sleep(2)

16
MetroX_CircuitPython/squeeze.py Executable file
View file

@ -0,0 +1,16 @@
"""
'squeeze.py'.
=================================================
force sensitive resistor (fsr) with circuitpython
"""
import analogio
import board
import pulseio
FORCE_SENS_RESISTOR = analogio.AnalogIn(board.A2)
LED = pulseio.PWMOut(board.D10)
while True:
LED.duty_cycle = FORCE_SENS_RESISTOR.value

View file

@ -0,0 +1,27 @@
"""
'temperature.py'.
=================================================
Writes TMP36 data to the REPL
"""
import time
import analogio
import board
from simpleio import map_range
temp_sensor = analogio.AnalogIn(board.A0)
def get_voltage(_temp_sensor):
"""gets the TMP36's voltage."""
voltage_val = map_range(_temp_sensor.value, 0, 65535, 0, 3.3)
return voltage_val
while True:
temp = get_voltage(temp_sensor)
# convert to celsius
temp = (temp - 0.5) * 100
print(" Temperature =", temp)
time.sleep(1)

View file

@ -0,0 +1,30 @@
"""
'temperature_alarm.py'.
=================================================
sounds an alarm when the temperature crosses a threshold
requires:
- simpleio
"""
import time
import analogio
import board
from simpleio import map_range, tone
tmp_36 = analogio.AnalogIn(board.A0)
freeze_temp = 0
boil_temp = 100
while True:
temp = map_range(tmp_36.value, 0, 65535, 0, 5)
# temp to degrees C
temp = (temp - 0.5) * 100
print(temp)
if temp < freeze_temp:
tone(board.D8, 349, 4)
if temp > boil_temp:
tone(board.D8, 523, 4)
time.sleep(0.5)

View file

@ -0,0 +1,45 @@
"""
'thermometer.py'.
=================================================
digital thermometer project using a tmp36 and a character lcd!
requires:
- simpleio
- CircuitPython_CharLCD Module
"""
import time
import digitalio
import analogio
import adafruit_character_lcd
from simpleio import map_range
from board import D7, D8, D9, D10, D11, D12, D13, A0
# Character LCD Config:
# modify this if you have a different sized charlcd
lcd_columns = 16
lcd_rows = 2
# Metro Express Pin Config:
lcd_rs = digitalio.DigitalInOut(D7)
lcd_en = digitalio.DigitalInOut(D8)
lcd_d7 = digitalio.DigitalInOut(D12)
lcd_d6 = digitalio.DigitalInOut(D11)
lcd_d5 = digitalio.DigitalInOut(D10)
lcd_d4 = digitalio.DigitalInOut(D9)
lcd_backlight = digitalio.DigitalInOut(D13)
# Init the lcd class
lcd = adafruit_character_lcd.Character_LCD(
lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight
)
therm = analogio.AnalogIn(A0)
while True:
# get temperature from sensor
tmp = ((map_range(therm.value, 0, 65535, 0, 3.3)) - 0.5) * 100
lcd.clear()
lcd.message("temp: " + str(tmp)[:5] + " * c")
time.sleep(0.6)

View file

@ -0,0 +1,22 @@
"""
'touch.py'
=================================================
capactive touch with the Adafruit Metro
requires:
- touchio
"""
import time
import board
import digitalio
import touchio
led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()
touch = touchio.TouchIn(board.A1)
while True:
if touch.value:
led.value = True
else:
led.value = False
time.sleep(1)

View file

@ -0,0 +1,25 @@
"""
'usb_scroll.py'.
=================================================
control a NeoPixel using an (NEC) IR Remote
requires:
- adafruit_hid
- simpleio
"""
import time
import analogio
import board
import digitalio
from adafruit_hid import mouse
from simpleio import map_range
button = digitalio.DigitalInOut(board.D6)
pot = analogio.AnalogIn(board.A0)
m = mouse.Mouse()
while True:
if not button.value: # move while button is pressed
m.move(0, 0, int(map_range(pot.value, 50, 65520, -5, 5)))
time.sleep(0.08)