├── .gitignore ├── 02-Blink └── main.py ├── 03-Digital-Input └── main.py ├── 04-Interrupts └── main.py ├── 05-ADC └── main.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | Production 2 | Old_Takes 3 | -------------------------------------------------------------------------------- /02-Blink/main.py: -------------------------------------------------------------------------------- 1 | # Learn MicroPython #2 - Blink 2 | 3 | from machine import Pin 4 | from time import sleep 5 | 6 | led = Pin(5, Pin.OUT) 7 | 8 | while True: 9 | led.on() 10 | sleep(1) 11 | led.off() 12 | sleep(1) -------------------------------------------------------------------------------- /03-Digital-Input/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | 3 | led = Pin(5, Pin.OUT) 4 | btn = Pin(0, Pin.IN) 5 | 6 | while True: 7 | if btn() == 0: 8 | led.on() 9 | else: 10 | led.off() -------------------------------------------------------------------------------- /04-Interrupts/main.py: -------------------------------------------------------------------------------- 1 | # Video: Learn MicroPython #4 - Interrupts 2 | 3 | from machine import Pin 4 | 5 | led = Pin(5, Pin.OUT) 6 | btn = Pin(0, Pin.IN) 7 | 8 | def my_func(pin): 9 | print("rising!") 10 | 11 | # Interrupt Request 12 | btn.irq(my_func, Pin.IRQ_RISING) 13 | -------------------------------------------------------------------------------- /05-ADC/main.py: -------------------------------------------------------------------------------- 1 | # Video: Learn MicroPython #5 - ADC 2 | 3 | # ESP32 has 18 ADC pins, but only 8 are usable with MicroPython, #32-39 4 | 5 | # Step One - import 6 | # ======== 7 | 8 | from machine import Pin, ADC 9 | 10 | # Step Two - create Pin 11 | # ======== 12 | 13 | pin34 = Pin(34, mode=Pin.IN) # setting to IN is optional since it is defualt 14 | adc = ADC(pin34) 15 | 16 | # ... or alternatively ... 17 | 18 | adc = ADC(Pin(34)) 19 | 20 | # Step Three - set voltage range ("attenuation") 21 | # ========== 22 | # There are four attenuation options. Most likely you will want to change 23 | # this setting to 11dB to use the full 3.6V range 24 | 25 | adc.atten(ADC.ATTN_0DB) # 0dB => 1.00V max (default) 26 | adc.atten(ADC.ATTN_2_5DB) # 2.5db => 1.34V max 27 | adc.atten(ADC.ATTN_6DB) # 6dB => 2.00V max 28 | adc.atten(ADC.ATTN_11DB) # 11db => 3.60V max 29 | 30 | # Step Four - set precision 31 | # ========= 32 | # There are four precision options. The default option is most precise, 33 | # so most likely you don't need to bother changing the width. 34 | 35 | adc.width(ADC.WIDTH_9BIT) # 9 bits => 0-511 36 | adc.width(ADC.WIDTH_10BIT) # 10 bits => 0-1023 37 | adc.width(ADC.WIDTH_11BIT) # 11 bits => 0-2047 38 | adc.width(ADC.WIDTH_12BIT) # 12 bits => 0-4095 (default) 39 | 40 | # Step Six - read raw ADC count 41 | # ======== 42 | 43 | value = adc.read() 44 | 45 | # Step Seven - convert to voltage 46 | # ========== 47 | # Remember that an ADC works by increasing an internal voltage in small steps 48 | # until it matches the voltage present on the pin, so the value read from the ADC is actually a count, not a voltage. 49 | 50 | # Assuming 12 bit precision and 11dB (3.6V) range: 51 | 52 | voltage = value / 4095.0 * 3.6 53 | 54 | print(voltage) 55 | 56 | # ... and now you're done. Here's a full example: 57 | 58 | # Example 59 | # ======= 60 | # Print voltage on pin 34 every second 61 | 62 | from machine import Pin, ADC 63 | from time import sleep 64 | 65 | adc = ADC(Pin(34)) 66 | adc.atten(ADC.ATTN_11DB) 67 | 68 | while True: 69 | value = adc.read() 70 | voltage = value / 4095.0 * 3.6 71 | 72 | print(voltage) 73 | 74 | time.sleep(1) 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # InventBox MicroPython Video Tutorial Course 2 | 3 | This repository contains the code used in the Learn MicroPython tutorial series, organized by video. 4 | 5 | __Watch the [video playlist](https://www.youtube.com/playlist?list=PL2935W76vRNEV6PwvxiqJf47JVrbGzTkC) on Youtube.__ 6 | 7 | 8 | --------------------------------------------------------------------------------