Skip to content

Latest commit

 

History

History
202 lines (131 loc) · 8.8 KB

octopus-micropython.md

File metadata and controls

202 lines (131 loc) · 8.8 KB

Octopus Sensor Modules with MicroPython

Contents

The example uses the ELECFREAKS Octopus Noise Sensor displayed below:

octopus-light-sensor

Example: Octopus Potentiometer

Step 1: Connect the Sensor to the Breakout Board

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.

micro:bit with Potentiometer


Step 2: Uploading External Modules

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.

Downloading octopus.py

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 github-download icon to download the file.

Uploading octopus.py into the micro:bit Python Editor.

Click on the Projects icon.

python-editor-file-upload-01

Click on the Open button.

python-editor-file-upload-02

Note

Replace main code with ... is the default. This will replace the code file flashed to the micro:bit.

python-editor-file-upload-03

Change the way the file is imported with the file settings icon file-settings-icon.

python-editor-file-upload-04

Change the option from Replace main code with octopus.py to Add file octopus.py.

python-editor-file-upload-04

The change is reflected in the dialog box.

python-editor-file-upload-04

Press the Confirm button.

python-editor-file-upload-04

The file octopus.py is now available as a Python module that can be imported.

python-editor-file-upload-04

The file now works with the micro:bit Python Editor code completion feature.

python-editor-file-upload-04


Step 3: Import the Modules

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

Step 4: Enable mirroring in Serial

This is not compulsory, but seeing the data being recorded shows that the code is working.

# Enable mirroring in serial
log.set_mirroring(True)

Step 5: Label the Columns on the MY_DATA.HTM File

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')

Step 6: Create an Instance of the Potentiometer Object

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)

Step 7: Log the Data

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)
    

Other ELECFREAKS Octopus Sensors

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 of from octopus import Potentiometer.
  • wl = WaterLevel(pin1) instead of p = 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 = Button(pin1)

button.get_presses()
Crash Sensor Crash Sensor Crash crash = Crash(pin1)

crash.get_presses()
Noise Sensor Noise Sensor Noise noise = Noise(pin1)

noise.get_noise()
Photocell Photocell Light Sensor Light light = Light(pin1)

light.get_light()
PIR Motion Sensor PIR Motion Sensor PIR pir = PIR(pin1)

pir.get_motion()
Potentiometer Potentiometer Potentiometer p = Potentiometer(pin1)

p.get_analog()
Soil Moisture Sensor Soil Moisture Sensor SoilMoisture sm = SoilMoisture(pin1)

sm.get_soil_moisture()
TMP36 TMP36 Temperature Sensor TMP36 temp = TMP36(pin1)

temp.get_temperature()
Ultrasonic Sensor Ultrasonic Distance Sensor Distance dist = Distance(pin1)

dist.get_distance()
UV Sensor UV Sensor UV uv = UV(pin1)

uv.get_uv()
Water Level Sensor Water Level Sensor WaterLevel wl = WaterLevel(pin1)

wl.get_water_level()