The example uses the ELECFREAKS Octopus Noise Sensor displayed below:
The breakout boards provided for this presentation may differ from the one presented below. The breakout boards from DFRobot have a green pin instead of a yellow pin for the GPIO. Nevertheless, this demonstration's functionality is the same, as the order is still SVG.
Connect the sensor to pin1 on the breakout board. The black pin (GND) should be connected to the black wire.
The log
module is a built-in module. The octopus
module is an external file that must be uploaded into the mico:bit Python Editor.
First, download the octopus module (octopus.py
) in the GitHub repository here. This is a collection of several official ELECFREAKS modules combined in one file for convenience. Other code that is not available from ELECTFREAKS has been added. This is a module that I created to make it easier for my students to use the sensors using MicroPython.
Click on the icon to download the file.
Click on the Projects icon.
Click on the Open button.
Note
Replace main code with ... is the default. This will replace the code file flashed to the micro:bit.
Change the way the file is imported with the file settings icon .
Change the option from Replace main code with octopus.py to Add file octopus.py.
The change is reflected in the dialog box.
Press the Confirm button.
The file octopus.py
is now available as a Python module that can be imported.
The file now works with the micro:bit Python Editor code completion feature.
Import the necessary modules that are not preloaded with import log
and from octopus import Potentiometer
as demonstrated below:
# Imports go at the top
from microbit import *
import log
from octopus import Potentiometer
This is not compulsory, but seeing the data being recorded shows that the code is working.
# Enable mirroring in serial
log.set_mirroring(True)
Set the name of the row labels on the log file.
# Label the light column on the MY_DATA.HTM file
log.set_labels('analog_val')
This is just good practice. pot
is a common name for a potentiometer variable, but this does not work well with students.
# Create an instance of the Potentiometer class
p = Potentiometer(pin1)
Log the data every 10 millisecond sleep(10)
in a while
loop while with the log.add()
method.
# Code in a 'while True:' loop repeats forever
while True:
# If button A is pressed log the potentiometer data
if button_a.is_pressed():
# Add a row to MY_DATA.HTM
log.add({
'analog_val': p.get_analog()
})
# If button B is pressed delete MY_DATA.HTM
if button_b.is_pressed():
# Delete MY_DATA.HTM
log.delete()
# Repeat every 10 milliseconds
sleep(10)
Here is the complete commented code for the Octopus Potentiometer.
# Imports go at the top
from microbit import *
import log
from octopus import Potentiometer
# Enable mirroring in serial
log.set_mirroring(True)
# Label the light column on the MY_DATA.HTM file
log.set_labels('analog_val')
# Create an instance of the Potentiometer class
p = Potentiometer(pin1)
# Code in a 'while True:' loop repeats forever
while True:
# If button A is pressed log the potentiometer data
if button_a.is_pressed():
# Add a row to MY_DATA.HTM
log.add({
'analog_val': p.get_analog()
})
# If button B is pressed delete MY_DATA.HTM
if button_b.is_pressed():
# Delete MY_DATA.HTM
log.delete()
# Repeat every 10 milliseconds
sleep(10)
Here are the sensors that are available to tinker with in this presentation. The code will be the same as above with a sensor-specific code. For example:
If the Octopus Water Level sensor is chosen use:
from octopus import WaterLevel
instead offrom octopus import Potentiometer
.wl = WaterLevel(pin1)
instead ofp = Potentiometer(pin1)
OR
Links to the .hex
files are included in the Octopus Sensor column:
Octopus Sensor | from octopus import ... |
Method for the Sensor | |
---|---|---|---|
Button | Button |
button = Button(pin1) button.get_presses() |
|
Crash Sensor | Crash |
crash = Crash(pin1) crash.get_presses() |
|
Noise Sensor | Noise |
noise = Noise(pin1) noise.get_noise() |
|
Photocell Light Sensor | Light |
light = Light(pin1) light.get_light() |
|
PIR Motion Sensor | PIR |
pir = PIR(pin1) pir.get_motion() |
|
Potentiometer | Potentiometer |
p = Potentiometer(pin1) p.get_analog() |
|
Soil Moisture Sensor | SoilMoisture |
sm = SoilMoisture(pin1) sm.get_soil_moisture() |
|
TMP36 Temperature Sensor | TMP36 |
temp = TMP36(pin1) temp.get_temperature() |
|
Ultrasonic Distance Sensor | Distance |
dist = Distance(pin1) dist.get_distance() |
|
UV Sensor | UV |
uv = UV(pin1) uv.get_uv() |
|
Water Level Sensor | WaterLevel |
wl = WaterLevel(pin1) wl.get_water_level() |