UBISS2024: Difference between revisions

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
Line 4: Line 4:
= Tasks =
= Tasks =


== Project 0: connect a Arduino Nano ESP32 board ==
== Task 0: connect an Arduino Nano ESP32 board ==
* Install the basic software https://labs.arduino.cc/en/labs/micropython   
* Install the basic software https://labs.arduino.cc/en/labs/micropython   
* connect the board via USB
* connect the board via USB
Line 11: Line 11:
* Control the external RGB LED (on, off, mix color, brightness)
* Control the external RGB LED (on, off, mix color, brightness)


=== solution Project 0: LED Blinking ===
=== Solution Task 0.1: LED Blinking ===
<syntaxhighlight lang="python" line='line'>
<syntaxhighlight lang="python" line='line'>
# Blinky example
# Blinky example
Line 29: Line 29:
</syntaxhighlight>
</syntaxhighlight>


=== solution Project 0: Control external RGB ===
=== Solution Task 0.2 Control external RGB ===
<syntaxhighlight lang="python" line='line'>
<syntaxhighlight lang="python" line='line'>
# RGB example
# RGB example
Line 58: Line 58:
</syntaxhighlight>
</syntaxhighlight>


== Project 1: read Acceleration from Arduino Nano ESP32 board ==
== Task 1: read Acceleration from Arduino Nano ESP32 board ==
* read data from the accelerometer and the gyro and print them (Arduino IDE) https://docs.arduino.cc/micropython/basics/board-examples/
* read data from the accelerometer and the gyro and print them (Arduino IDE) https://docs.arduino.cc/micropython/basics/board-examples/
* extend you program to write the data from the accelerometers to a file, https://www.sketching-with-hardware.org/wiki/FileIO
* extend you program to write the data from the accelerometers to a file, https://www.sketching-with-hardware.org/wiki/FileIO
Line 64: Line 64:
* optional: add the photo resistors to your board, read their values, and write them to the file, too, https://www.sketching-with-hardware.org/wiki/LDR
* optional: add the photo resistors to your board, read their values, and write them to the file, too, https://www.sketching-with-hardware.org/wiki/LDR


=== solution Project 1: Read Accelerometer and Gyro ===
=== Solution Task 1.1: Read Accelerometer and Gyro ===
<syntaxhighlight lang="python" line='line'>
<syntaxhighlight lang="python" line='line'>
import time
import time
Line 82: Line 82:




 
=== Solution Task 1.2: Read analog values - Code Example Arduino Nano Connect RP2040 ===
=== solution Project 2: Read analog values - Code Example Arduino Nano Connect RP2040 ===
A0 is the analog input with 16 bit resolution. It reads the analog value every second and print it to the console-
A0 is the analog input with 16 bit resolution. It reads the analog value every second and print it to the console-


Line 99: Line 98:
</syntaxhighlight>
</syntaxhighlight>


== Project 2: Jupyter Notebook ==
== Task 2: Jupyter Notebook ==
* connect the board
* connect the board
* install the Juypter Notebook, https://www.sketching-with-hardware.org/wiki/Jupyter
* install the Juypter Notebook, https://www.sketching-with-hardware.org/wiki/Jupyter

Revision as of 09:44, 10 June 2024

Link Page

https://www.sketching-with-hardware.org/wiki/UBISS2024-Links

Tasks

Task 0: connect an Arduino Nano ESP32 board

Solution Task 0.1: LED Blinking

# Blinky example

import time
from machine import Pin

# This is the only LED pin available on the Nano RP2040,
# other than the RGB LED connected to Nano WiFi module.
led = Pin(6, Pin.OUT)

while (True):
   led.on()
   time.sleep_ms(250)
   led.off()
   time.sleep_ms(200)

Solution Task 0.2 Control external RGB

# RGB example

import time
from machine import Pin

# RGB LED connected to Nano WiFi module.
ledG = Pin(2, Pin.OUT)
ledR = Pin(3, Pin.OUT)
ledB = Pin(4, Pin.OUT)
print("start")

while (True):
    print("*")
    ledG.on()
    ledR.off()
    ledB.off()
    time.sleep_ms(250)
    ledG.off()
    ledR.on()
    ledB.off()
    time.sleep_ms(250)
    ledG.off()
    ledR.off()
    ledB.on()
    time.sleep_ms(250)

Task 1: read Acceleration from Arduino Nano ESP32 board

Solution Task 1.1: Read Accelerometer and Gyro

import time
from lsm6dsox import LSM6DSOX

from machine import Pin, I2C
lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12)))

while (True):
    accel_data = lsm.accel()
    print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*accel_data))
    gyro_data = lsm.gyro()
    print('Gyroscope:     x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*gyro_data))
    print("")
    time.sleep_ms(100)


Solution Task 1.2: Read analog values - Code Example Arduino Nano Connect RP2040

A0 is the analog input with 16 bit resolution. It reads the analog value every second and print it to the console-

#Example usage for Arduino Nano
from machine import Pin, ADC
from time import sleep

analogPin = ADC(Pin(26))

while True:
  analogVal16 = analogPin.read_u16()
  print(analogVal16)
  sleep(1)

Task 2: Jupyter Notebook


Task 2.1: is it moved?

  • read acceleration and gyro
  • calculate the differences between values
  • show an ouput when it is move
  • create a file on the device that logs, when it is moved

Task 2.2: it was turned upside down?

  • read acceleration and gyro
  • make a rule based "AI" that records
    • it was put upside down
    • it was turned 360
    • it was moved "quickly"

Task 3: ML on Arduino Nano Connect RP2040

We will use https://github.com/eloquentarduino/everywhereml to detect the same gestures as in Task 2.2. For this, install everywhereml:

pip3 install -U everywhere

Using everywhereml we can train a model on a more powerful machine for deployment on a microcontroller. See https://eloquentarduino.com/posts/micropython-machine-learning for example for such a training process. Assuming that our ML model is trained and stored in variable clf then we can save the model to a file using

clf.to_micropython_file("MyModel.py")

The MyModel.py file can then be saved and called directly on the microcontroller. To run the model on the microcontroller, assume your data is stored in x and you trained a RandomForestClassifier. Then you can predict via the following code snippet

import MyModel
clf = MyModel.RandomForestClassifier()
clf.predict(x)

Task 4: connect both boards to WIFI

  • connect both boards to WIFI using Tutorial_Network
  • use the Arduino Nano ESP32 as output (showing a color)
  • use the Arduino Nano Connect RP2040 as input (recognize with rules 3 gestures)

Links

See the full list of links: UBISS2024-Links

Local Links

https://ubicomp.net/sw/db1/var2db.php? http://localhost:8888/notebooks/ArduinoNanoRP2040_v01.ipynb http://localhost:8888/doc/tree/create-ML-model01.ipynb

Reading

Required Reading before the course

Recommended Reading before the course

Random Commands

pip install micropython-lsm6dsox

picotool.exe load -x C:\Users\ru42qak\AppData\Roaming\OpenMV\openmvide\firmware\ARDUINO_NANO_RP2040_CONNECT\firmware.bin

pip install jupyterlab

pip install everywhereml

python -m pip install jupyter

git clone https://github.com/goatchurchprime/jupyter_micropython_kernel.git

pip install -e jupyter_micropython_kernel

python -m notebook

python -m jupyter kernelspec list


C:\Users\ru42qak\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\jupyterlab>pip install -e jupyter_micropython_kernel

C:\Users\ru42qak\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\jupyterlab>python -m notebook