Skip to content

mithru/MicroPython-Examples

Repository files navigation

MicroPython-Examples

This repo features examples for MicroPython similar to Arduino.

Troubleshooting

Safe Mode
Hold down USR switch and quickly press and release RST switch. Release USR switch when only the orange LED is lit. The orange LED will flash quickly 4 times and stop. This is safe mode, which ignores boot.py and main.py. It might be helpful if something is running on the pyboard and that's preventing you from changing its contents.

Factory Reset
Hold down USR switch and quickly press and release RST switch. Release USR switch when both orange and green LEDs are lit. Both those LEDs will flash 4 times and stop. The board will now be factory reset.

00.Basics

boot.py
Sample boot file which points to 'helloWorld.py' to run after booting. The examples in this repo will not include copies of the boot file. Please edit and include boot.py accordingly.

helloWorld.py
Turns on LED 4 (the blue LED).

REPL
REPL stands for Read-Eval-Print-Loop.
Very important and highly useful feature that allows easy debugging and quick learning.
All examples can be tested on the board without having to manually upload every time.


01.LEDs

blink
Replicates Arduino's Blink sketch exactly (uses .on() and .off() and loops)

blinkWithoutDelay
Replicates Arduino's BlinkWithoutDelay sketch (uses .toggle() and .millis())

blinkToggle
Same as Blink, but uses .toggle()

fade
Similar to Arduino's Fade sketch

heartbeat
Something more interesting that simple blinks

heartbeatFade
Similar to previous 'Heartbeat' script, but fades the light in and out instead of just switching on and off


02.Inputs

button
Pull down button that toggles an LED

potentiometer
Simple analog input that controls an LED's intensity

switchObject
Simple demo of the Switch object that can control the inbuilt USR button on the pyboard

switchCallback
Example of the Switch.callback function (uses interrupts)


03.Pins

PinsBasicOutput
Example of using on of the pins to control an output (LED, motor, buzzer, relay, et al)


04.Accelerometer

accelerometerControlLED
Controls blink speed of an LED using the accelerometer's value along the X axis
This script also features a custom remap() function that remaps a value to different bounds/range

accelerometer
Prints the x,y,z values of the accelerometer every second (REPL required)


05.Servos

ServoSetAngle
Example of setting a servo's angle, and animating it over time

ServoGetAngle
Generates a random number roughly between -90 and 90, and sets it as the servo's angle. The servo's angle is then obtained from the object


06.Clock

clock
Uses the RTC (independent Real Time Clock) in the pyboard. Prints the time and date with delay of 100 microseconds. Start time is set with variables.


07.Mouse

boot.py
Sets the device as a HID when it boots.

main.py Control cursor with the pyboard's accelerometer. The USR switch is used as the button. Hold the board such that side with the microUSB port points to the direction you face.


08.Sensors

HC-SR04
Prints distance values in cm and inches from two ultrasonic sensors connected to the pyboard. Uses the ultrasonic module available here and here.


Contributions include code from:
Mithru Vigneshwara
Dave Hylands

Releases

No releases published

Packages

No packages published

Languages