2017年2月6日 星期一

MicroPython - Flashing LED Tutorial - Ninja GeckoNinja Gecko


http://www.ninjagecko.co.uk/micropython-flashing-led-tutorial/



For this project you will need the following:
  • MicroPython Microcontroller with headers soldered on ( or a soldering iron)
  • 2 Jumper Leads
  • Breadboard
  • 1 LED
  • 1 560 ohm resistor

Step 1 – Wire up your LED and resistor.

The first thing you need to do is wire up your LED and resistor circuit as shown below!
flashing LED breadboard view
Resistors can be connected either way around.
Need help calculating the color code for the resistor? Click here!

Step 2 – Connect your MicroPython Controller to the Computer.

Once your have completed the wiring you now need to plug your MicroPython in to your computer using a micro USB cable. This serves 2 purposes:
  1. It provides power to the MicroPython
  2. It gives you access to the Python main.py file that we need to edit.

Step 3 – Open the main.py file and add your code

Your PC should have automatically recognised the MicroPython as a USB device and you should have access to a new drive that should look something like below ( if you are using Windows).
pyb flash icon
Double click on the drive and then open file main .py in notepad or another editor.
mainpyicon
Python Code
import time
from pyb import Pin
led1 = Pin("Y1",Pin.OUT_PP)
while True:
    led1.high()
    time.sleep(1)
    led1.low()
    time.sleep(1)
Python Code with Comments
import time #Go get the time module
from pyb import Pin #Go get the Pin class from the pyb module
led1 = Pin("Y1",Pin.OUT_PP) #Create a new instance of the Pin class and set is to output
while True: # Create an infinite loop
    led1.high() #Turn the LED on
    time.sleep(1) #Wait 1 second
    led1.low() #Turn the LED off
    time.sleep(1) #Wait 1 second

Step 4 – Save your code

Save your changes to the code and then wait until the red light on your MicroPython Turns off – this means it has finished saving!

Step 5 – Reset your MicroPython

Press the reset button and your LED should flash!
reset

Troubleshooting

  • Make sure you have typed ‘Y1’ not ‘y1’ – The capitalisation is important!
  • Take care that you wait for the red light to turn off before pressing the reset button!

沒有留言:

張貼留言

零成本學 ESP32!在 Wokwi 打造你的第一個微型氣象站 (ESP32 + DHT22 + 1602 LCD)

零成本學 ESP32!在 Wokwi 打造你的第一個微型氣象站(溫度 濕度) (ESP32 + DHT22 + 1602 LCD) 想學物聯網(IoT)與 ESP32 電子實作,卻擔心接線與電壓規範搞砸嗎?透過強大的線上模擬器 Wokwi ,我們不需要花費任何硬體費用,打開瀏覽器...