Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@AnnaGerber
Last active October 16, 2022 13:51
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AnnaGerber/26decdf2aa53150f7515 to your computer and use it in GitHub Desktop.
Save AnnaGerber/26decdf2aa53150f7515 to your computer and use it in GitHub Desktop.
PyFirmata code for Freetronics Experimenter's Kit
import pyfirmata
import math
board = pyfirmata.Arduino("/dev/ttyACM0")
# connect piezo to pin 9 to use PWM
SENSOR_PIN = 0
PIEZO_PIN = board.get_pin('d:9:p')
it = pyfirmata.util.Iterator(board)
it.start()
board.analog[SENSOR_PIN].enable_reporting()
# check buzzer is working
PIEZO_PIN.write(0.2)
board.pass_time(0.5)
PIEZO_PIN.write(0.6)
board.pass_time(0.5)
PIEZO_PIN.write(0.8)
board.pass_time(0.5)
PIEZO_PIN.write(0)
while True:
light_level = board.analog[SENSOR_PIN].read()
if (light_level != None):
# you may wish to adjust the raw reading here to get a better range of beeps
buzzer_value = light_level
print "setting piezo to %s" % buzzer_value
PIEZO_PIN.write(buzzer_value)
board.pass_time(0.5)
PIEZO_PIN.write(0)
board.pass_time(0.5)
import pyfirmata
board = pyfirmata.Arduino("/dev/ttyACM0")
ledPins = [6,7,8,9,10,11,12,13]
while True:
for x in range(0,8):
print "lighting up led on pin %i" % ledPins[x]
board.digital[ledPins[x]].write(1)
board.pass_time(1)
board.digital[ledPins[x]].write(0)
board.pass_time(1)
import pyfirmata
board = pyfirmata.Arduino("/dev/ttyACM0")
button = board.get_pin('d:2:i')
it = pyfirmata.util.Iterator(board)
it.start()
button.enable_reporting()
while True:
if str(button.read()) == 'True':
print "Pressed!"
board.pass_time(1)
import pyfirmata
board = pyfirmata.Arduino("/dev/ttyACM0")
SENSOR_PIN = 0
LED_PIN = 13
it = pyfirmata.util.Iterator(board)
it.start()
board.analog[SENSOR_PIN].enable_reporting()
while True:
light_level = board.analog[SENSOR_PIN].read()
print "Reading from analog sensor: %s" % light_level
board.pass_time(1)
# EXERCISE add blinking of LEDs in response to reading
# delay = ...
# board.digital[LED_PIN].write(1)
# board.pass_time(delay)
# board.digital[LED_PIN].write(0)
import pyfirmata
DELAY = 0.5
brightness = 0
board = pyfirmata.Arduino("/dev/ttyACM0")
led = board.get_pin('d:11:p')
# increase
for i in range(0, 10):
brightness = brightness + 0.1
print "Setting brightness to %s" % brightness
led.write(brightness)
board.pass_time(DELAY)
# decrease
for i in range(0, 10):
print "Setting brightness to %s" % brightness
led.write(brightness)
brightness = brightness - 0.1
board.pass_time(DELAY)
board.exit()
import pyfirmata
DELAY = 1
MIN = 5
MAX = 175
MID = 90
board = pyfirmata.Arduino("/dev/ttyACM0")
servo = board.get_pin('d:11:s')
def move_servo(v):
servo.write(v)
board.pass_time(DELAY)
move_servo(MIN)
move_servo(MAX)
move_servo(MID)
board.exit()
import pyfirmata
import random
board = pyfirmata.Arduino("/dev/ttyACM0")
RED_PIN = board.get_pin('d:9:p')
GREEN_PIN = board.get_pin('d:10:p')
BLUE_PIN = board.get_pin('d:11:p')
# light up red, green, blue
RED_PIN.write(1)
board.pass_time(0.5)
RED_PIN.write(0)
GREEN_PIN.write(1)
board.pass_time(0.5)
GREEN_PIN.write(0)
BLUE_PIN.write(1)
board.pass_time(0.5)
# randomly change colour every half second
while True:
RED_PIN.write(random.random())
GREEN_PIN.write(random.random())
BLUE_PIN.write(random.random())
board.pass_time(0.5)
@Python6626
Copy link

Thank you very much. I just started. It helped me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment