Skip to content

Commit bd0c5d3

Browse files
update examples for testing of final fw image
1 parent a55d1fc commit bd0c5d3

17 files changed

+222
-107
lines changed

source_only_examples/project1_a.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1+
# MicroPython SparkFun Inventor's Kit Circuit 1A: Blink an LED
2+
3+
# What you should see:
4+
# When this code runs, it blinks the LED 10 times by setting the LED high, then low and sleeping in between each step.
5+
#
6+
# For more information, check out the guide here: TODO: guide URL
7+
8+
# To blink the LED, we need to enable the board pin 34 (the pin that the LED is connected to in the circuit).
9+
# To do this we load the Pin definition for the board
110
from machine import Pin
211

12+
# Now create a Pin variable for the LED pin, number 34. Also define it as an output pin, so we can turn it on and off
313
led_pin = Pin(34, Pin.OUT)
4-
led_pin.high()
5-
led_pin.low()
614

15+
# We turn the LED on by setting the pin value to high or on
16+
# And to turn the LED off, we set the pin to low like so:
17+
# led_pin.high()
18+
# led_pin.low()
19+
20+
# To blink the LED, we can turn it on, wait a period of time and then turn it off. This is done by sleeping between the on and off commands.
21+
22+
# To do this in MicroPython, we need a sleep function. Let’s load the sleep function, which will sleep for a number of seconds.
723
from time import sleep
8-
led_pin.high()
9-
sleep(1)
10-
led_pin.low()
11-
sleep(1)
12-
led_pin.high()
13-
sleep(1)
14-
led_pin.low()
1524

25+
# Now lets blink the LED - sleeping for 1 second between turning the LED on and off
26+
# Now we’ll present the idea of a for loop. A loop repeats a statement for a number of specified times.
27+
28+
# To blink our LED 10 times, use the following commands:
1629
for i in range(10):
1730
led_pin.high()
1831
sleep(1)

source_only_examples/project1_b.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
# MicroPython SparkFun Inventor's Kit Circuit 1B: Potentiometer
2+
3+
# What you should see:
4+
# You should see the LED blink faster or slower in accordance with your potentiometer. The delay between each flash will change based on the position of the knob.
5+
#
6+
# For more information, check out the guide here: TODO: guide URL
7+
18
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
29

310
from machine import ADC # Allows us to use "ADC" (analog-to-digital conversion) to read from our analog pin
411

512
led_pin = Pin(34, Pin.OUT) # Create a pin variable for the led pin (pin 34)
613
potentiometer = ADC(Pin.board.A0) # Create an ADC variable for reading the potentiometer value from analog pin A0
714

8-
# Try moving the potentiometer and re-running this cell and you should see this value change.
9-
print(potentiometer.read_u16()) # Use the "read_u16" method to read the value of our potentiometer.
15+
# Try moving the potentiometer and running the below line repeatedly and you should see the value change
16+
# print(potentiometer.read_u16()) # Use the "read_u16" method to read the value of our potentiometer.
1017

1118
# Now, let's blink the LED with different speeds based on the potentiometer input
1219
import time # Allows us to use "time.sleep()" to delay for a certain number of seconds

source_only_examples/project1_c.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
1+
# MicroPython SparkFun Inventor's Kit Circuit 1C: Photoresistor
2+
#
3+
# What you should see:
4+
# The program stores the light level in a variable. Then, using an if/else statement, the program checks to see what it should do with the LED.
5+
# If the variable is above the threshold (it’s bright), turn the LED off. If the variable is below the threshold (it’s dark), turn the LED on.
6+
# You now have just built your own night-light!
7+
#
8+
# For more information, check out the guide here: TODO: guide URL
9+
110
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
211

312
from machine import ADC # Allows us to use "ADC" (analog-to-digital conversion) to read from our analog pin
413

514
led_pin = Pin(34, Pin.OUT) # Create a pin variable for the led pin (pin 34)
615
photoresistor = ADC(Pin.board.A0) # Create an ADC variable for reading the photoresistor value from analog pin A0
716

8-
# Try moving the potentiometer and re-running this cell and you should see this value change.
9-
print(photoresistor.read_u16()) # Use the "read_u16" method to read the value of our potentiometer.
17+
# Try moving the potentiometer and running the below line repeatedly and you should see the value change
18+
# print(photoresistor.read_u16()) # Use the "read_u16" method to read the value of our potentiometer.
1019

1120
# Now, let's turn the LED on and off based on the photoresistor value.
1221
import time # Allows us to use "time.sleep()" to delay for a certain number of seconds
1322

14-
# We'll set our threshold to half of the maximum value of the ADC reading (65535)
15-
threshold = 65535 / 2
23+
# We'll set our threshold to a bit below the top of the range of the photoresistor (65535)
24+
# you can change this as you want to match the lighting in your room
25+
threshold = 55000
1626

1727
# Infinite loop so this cell keeps running until we stop it.
1828
while True:
1929
photoValue = photoresistor.read_u16() # Get the new photoresistor value (0 - 65535)
2030

2131
print(f"Photoresistor Value: {photoValue : 5}", end='\r') # Print our Photoresistor reading (don't mind the fanciness of this line it just makes the print format nicely)
2232

23-
# Turn on the LED but only if the photoresistor value is above the threshold
24-
if photoValue > threshold:
33+
# Turn on the LED but only if the photoresistor value is below the threshold
34+
if photoValue < threshold:
2535
led_pin.high()
2636
else:
2737
led_pin.low()
2838

2939
# A short delay to make the printout easier to read
3040
time.sleep(0.250)
31-

source_only_examples/project1_d.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# MicroPython SparkFun Inventor's Kit Circuit 1D: RGB Night-Light
2+
#
3+
# What you should see:
4+
# This program puts together the photoresistor, potentiometer, and RGB led to create a customizable night light.
5+
# When the light of your room is below the threshold, the RGB will turn on.
6+
# The color of the RGB can then be set with the potentiometer.
7+
#
8+
# For more information, check out the guide here: TODO: guide URL
9+
110
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
211

312
from machine import ADC # Allows us to use "ADC" (analog-to-digital conversion) to read from our analog pin
@@ -68,8 +77,9 @@ def turnOff():
6877
photoresistor = ADC(Pin.board.A0) # Create an ADC variable for reading the photoresistor value from analog pin A0
6978
potentiometer = ADC(Pin.board.A1) # Create an ADC variable for reading the potentiometer value from analog pin A1
7079

71-
# We'll set our photo-resistor threshold to a quarter of the maximum value of the ADC reading (65535)
72-
threshold = 65535 / 4
80+
# We'll set our threshold to a bit below the top of the range of the photoresistor (65535)
81+
# you can change this as you want to match the lighting in your room
82+
threshold = 55000
7383
potentiometerMax = 65535 # Maximum value for the potentiometer reading
7484

7585
# Infinite loop to continously read the photoresistor and potentiometer values

source_only_examples/project2_a.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# MicroPython SparkFun Inventor's Kit Circuit 2A: Buzzer
2+
3+
# What you should see:
4+
# When the program begins, a song will play from the buzzer once. Use the potentiometer to adjust the volume.
5+
#
6+
# For more information, check out the guide here: TODO: guide URL
7+
18
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
29
from machine import PWM # Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED
310

source_only_examples/project2_b.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# MicroPython SparkFun Inventor's Kit Circuit 2B: Digital Trumpet
2+
#
3+
# What you should see:
4+
# Different tones will play when you press different keys. Turning the potentiometer will adjust the volume.
5+
#
6+
# For more information, check out the guide here: TODO: guide URL
7+
18
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
29
from machine import PWM # Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED
310

source_only_examples/project2_c.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# MicroPython SparkFun Inventor's Kit Circuit 2C: Simon Says Game
2+
#
3+
# What you should see:
4+
# The circuit will flash all of the LEDs and play a melody. After a few seconds, it will flash the first light in the pattern.
5+
# If you repeat the pattern correctly by pressing the corresponding colored button, then the game will move to the next round and add another color to the pattern sequence.
6+
# If you make a mistake, the loss melody will play. If you get to round 10, the win melody will play. Press any button to start a new game.
7+
#
8+
# For more information, check out the guide here: TODO: guide URL
9+
110
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
211
from machine import PWM # Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED
312

source_only_examples/project3_a.py

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,12 @@
1-
# TODO: Depending on how we want this to look, we may not include this (in case it is in firmware) but for now
2-
# this will get it to work.
3-
from machine import PWM, Pin, ADC
4-
import math
1+
# MicroPython SparkFun Inventor's Kit Circuit 3A: Servo Motors
2+
#
3+
# What you should see:
4+
# Turning the potentiometer will cause the servo to turn.
5+
#
6+
# For more information, check out the guide here: TODO: guide URL
57

6-
class Servo:
7-
def __init__(self,pin_id,min_us=544.0,max_us=2400.0,min_deg=0.0,max_deg=180.0,freq=50):
8-
self.pwm = machine.PWM(machine.Pin(pin_id))
9-
self.pwm.freq(freq)
10-
self.current_us = 0.0
11-
self._slope = (min_us-max_us)/(math.radians(min_deg)-math.radians(max_deg))
12-
self._offset = min_us
13-
14-
def write(self,deg):
15-
self.write_rad(math.radians(deg))
16-
17-
def read(self):
18-
return math.degrees(self.read_rad())
19-
20-
def write_rad(self,rad):
21-
self.write_us(rad*self._slope+self._offset)
22-
23-
def read_rad(self):
24-
return (self.current_us-self._offset)/self._slope
25-
26-
def write_us(self,us):
27-
self.current_us=us
28-
self.pwm.duty_ns(int(self.current_us*1000.0))
29-
30-
def read_us(self):
31-
return self.current_us
32-
33-
def off(self):
34-
self.pwm.duty_ns(0)
8+
from machine import Pin, ADC
9+
from sik_utilities import Servo # Import the servo class from the sik_utilities file (it uses PWM under the hood)
3510

3611
led_pin = Pin(34, Pin.OUT) # Create a pin variable for the led pin (pin 34)
3712
potentiometer = ADC(Pin.board.A0) # Create an ADC variable for reading the potentiometer value from analog pin A0

source_only_examples/project3_b.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# MicroPython SparkFun Inventor's Kit Circuit 3B: Distance Sensor
2+
#
3+
# What you should see:
4+
# Move your hand or a large, flat object closer and farther away from the distance sensor. As the object approaches, the light will change from green to yellow to red.
5+
#
6+
# For more information, check out the guide here: TODO: guide URL
7+
18
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
29
from machine import PWM # Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED
310
from time import sleep_us, sleep

source_only_examples/project3_c.py

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,16 @@
1-
# TODO: Depending on how we want this to look, we may not include this (in case it is in firmware) but for now
2-
# this will get it to work.
3-
import machine
4-
import math
5-
from time import sleep_us
1+
# MicroPython SparkFun Inventor's Kit Circuit 3C: Motion Alarm
2+
#
3+
# What you should see:
4+
# The RGB LED will behave as in your last circuit. It will be green when objects are far, yellow when they are midrange and red when they are close.
5+
# When an object is close the buzzer will also beep, and the servo will rotate back and forth.
6+
#
7+
# For more information, check out the guide here: TODO: guide URL
8+
9+
from time import sleep_us, sleep
610
from machine import time_pulse_us
711
from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board
812
from machine import PWM # Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED
9-
10-
11-
class Servo:
12-
def __init__(self,pin_id,min_us=544.0,max_us=2400.0,min_deg=0.0,max_deg=180.0,freq=50):
13-
self.pwm = machine.PWM(machine.Pin(pin_id))
14-
self.pwm.freq(freq)
15-
self.current_us = 0.0
16-
self._slope = (min_us-max_us)/(math.radians(min_deg)-math.radians(max_deg))
17-
self._offset = min_us
18-
19-
def write(self,deg):
20-
self.write_rad(math.radians(deg))
21-
22-
def read(self):
23-
return math.degrees(self.read_rad())
24-
25-
def write_rad(self,rad):
26-
self.write_us(rad*self._slope+self._offset)
27-
28-
def read_rad(self):
29-
return (self.current_us-self._offset)/self._slope
30-
31-
def write_us(self,us):
32-
self.current_us=us
33-
self.pwm.duty_ns(int(self.current_us*1000.0))
34-
35-
def read_us(self):
36-
return self.current_us
37-
38-
def off(self):
39-
self.pwm.duty_ns(0)
13+
from sik_utilities import Servo # Import the servo class from the sik_utilities file (it uses PWM under the hood)
4014

4115
# LEDs
4216
pwmRed = PWM(Pin(28), freq=1000, duty_u16=0) # Create a PWM object on pin 28 with a frequency of 1000Hz and an initial "on time" of 0 (off)
@@ -53,9 +27,6 @@ def off(self):
5327
# Servo
5428
myServo = Servo(pin_id=35)
5529

56-
from time import sleep_us
57-
from machine import time_pulse_us
58-
5930
def get_distance():
6031
trigPin.high()
6132
sleep_us(10) # Send at least a 10 microsecond pulse to the trigger pin to start the measurement
@@ -67,8 +38,6 @@ def get_distance():
6738

6839
return calculatedDistance # Return the calculated distance
6940

70-
from time import sleep
71-
7241
# Infinite loop to read the distance sensor and update the RGB LED colors based on the distance
7342
while True:
7443
distance = get_distance() # Get the distance from the sensor

0 commit comments

Comments
 (0)