MJRoBot (Marcelo Rovai)
Published © GPL3+

MicroPython on ESP Using Jupyter

Let's play with MicroPython on an ESP using Jupyter Notebook. Getting data from sensors and taking action in a physical world.

BeginnerFull instructions provided6 hours3,851
MicroPython on ESP Using Jupyter

Things used in this project

Story

Read more

Schematics

Electrical Diagram

Code

Code snippet #18

Plain text
# import library to deal with pins:
from machine import Pin

# define pin 0 as output
led = Pin(0, Pin.OUT)

# define value of "led" as "1" or "True" to turn on the LED
led.value(1)

# define value of "led" as "0" or "False" to turn off the LED
led.value(0)

# also you can use .on() or .off methods to control the pin:
led.on()

led.off()

Code snippet #19

Plain text
from time import sleep

for i in range(5):
    led.on()
    sleep(0.5)
    led.off()
    sleep(0.5)

Code snippet #20

Plain text
# define pin 13 as an input and activate an internal Pull-up resistor:
button = Pin(13, Pin.IN, Pin.PULL_UP)

# Read button state:
print(button.value())

Code snippet #24

Plain text
print (button.value())
if button.value() == 0:
    led.on()
else:
    led.off()

Code snippet #26

Plain text
pwm0 = PWM(Pin(0))      # create PWM object from a pin
pwm0.freq()             # get current frequency
pwm0.freq(1000)         # set frequency
pwm0.duty()             # get current duty cycle
pwm0.duty(200)          # set duty cycle
pwm0.deinit()           # turn off PWM on the pin

Code snippet #28

Plain text
from machine import Pin, PWM
pwm0 = PWM(Pin(0), freq=1000, duty=0)
for i in range (0,1023,20):
    pwm0.duty(i)
    sleep(0.1)
pwm0.duty(0)
pwm0.deinit() 

Code snippet #30

Plain text
# Minimum position (angle 0)
servo.duty(40)

# Maximun position (angle 180)
servo.duty(40)

# center position (angle 90)
servo.duty(40)

Code snippet #31

Plain text
# swipping servo
step = 2
for i in range (40, 115, step):
    servo.duty(i)
    sleep (0.1)
step = -1*step
for i in range (115, 40, step):
    servo.duty(i)
    sleep (0.1)
servo.duty(77)

Code snippet #41

Plain text
data.measure()
temp = data.temperature()
hum = data.humidity()
print('Temp: {}oC'.format(temp))
print('Hum:  {}%'.format(hum))

Code snippet #44

Plain text
def scanI2c():
    print('Scan i2c bus...')
    devices = i2c.scan()

    if len(devices) == 0:
        print("No i2c device !")
    else:
        print('i2c devices found:',len(devices))

    for device in devices:  
        print("Decimal address: ",device," | Hexa address: ",hex(device))

Code snippet #47

Plain text
- poweroff(), turns off the screen. Convenient for battery operation.
- contrast(), to adjust the contrast
- invert(), invert the colors of the screen (finally white and black!)
- show(), to refresh the view
- fill(), to fill the screen in black (1) or white (0)
- pixel(), to turn on a particular pixel
- scroll(), scroll the screen.
- text(), to display on text at the indicated x, y position
- Draw lines hline(), vline() or any line line()
- Draw a rect rect rectangle() or rectangle filled fill_rect()

Github file

https://github.com/jupyter/jupyter/wiki/Jupyter-kernels

Github file

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

Github

https://github.com/adafruit/ampy

Github file

https://github.com/Mjrovai/Python4DS/tree/master/Micropython

Credits

MJRoBot (Marcelo Rovai)

MJRoBot (Marcelo Rovai)

66 projects • 956 followers
Professor, Engineer, MBA, Master in Data Science. Writes about Electronics with a focus on Physical Computing, IoT, ML, TinyML and Robotics.

Comments