MicroPython and CPython (Raspberry Pi) – Basic
Hardware Control Notes
1. MicroPython Overview
MicroPython is a lightweight implementation of Python designed for microcontrollers
like ESP32 and ESP8266.
It allows direct hardware control using modules such as machine.
Import Hardware Modules
from machine import Pin
import time
Pin Control (Output)
from machine import Pin
led = Pin(2, [Link])
LED Control
[Link]()
[Link]()
# Alternative
[Link](1)
[Link](0)
Digital Input
button = Pin(4, [Link])
value = [Link]()
Delay
import time
[Link](1)
ADC (Analog Read)
from machine import ADC, Pin
adc = ADC(Pin(34))
value = [Link]()
PWM Example
from machine import Pin, PWM
pwm = PWM(Pin(2))
[Link](1000)
[Link](512)
I2C Example
from machine import I2C, Pin
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
UART Example
from machine import UART
uart = UART(2, baudrate=9600)
LED Blink (MicroPython)
from machine import Pin
import time
led = Pin(2, [Link])
while True:
[Link]()
[Link](1)
[Link]()
[Link](1)
2. CPython on Raspberry Pi Overview
CPython is the standard Python interpreter used on computers and on Raspberry Pi.
GPIO pins are controlled using the [Link] library.
Import Library
import [Link] as GPIO
import time
Pin Numbering
[Link]([Link])
Set Output Pin
[Link](17, [Link])
LED Control
[Link](17, True)
[Link](17, False)
Digital Input
[Link](4, [Link])
value = [Link](4)
Delay
[Link](1)
PWM Example
pwm = [Link](17, 1000)
[Link](50)
LED Blink (Raspberry Pi)
import [Link] as GPIO
import time
[Link]([Link])
[Link](17, [Link])
while True:
[Link](17, True)
[Link](1)
[Link](17, False)
[Link](1)
Arduino vs MicroPython vs Raspberry Pi Python
Function Arduino C MicroPython Raspberry Pi Python
pinMode pinMode() Pin() [Link]()
digitalWrite digitalWrite() [Link]()/value() [Link]()
digitalRead digitalRead() [Link]() [Link]()
delay delay() [Link]() [Link]()
analogRead analogRead() [Link]() External ADC required