Testing MicroPython with LEDs

MicroPython is now installed on our ESP32 module, but before we start to set up Wi-Fi and build a more complex application, it's always a good idea to start with something simple and then build up the complexity. In the software world, we often print Hello World as a sanity check, but in the hardware world, blinking an LED is a great test.

For a quick test, connect an LED with a 220-ohm series resistor to pin 2 of the ESP32 Devkit-C connect. I connect the resistor to the 3.3 volts pin and the LED anode and then the LED cathode directly to pin 2. When I say pin 2 in this case, I mean the pin that is labeled with the number 2 on the board solder mask. This doesn't represent the physical pin 2 but the I/O pin 2.

Once you have the LED connected, open your serial prompt so that you are in the MicroPython REPL. Create the following function in the REPL:

def toggle(p):
p.value(not p.value())

Make sure that you press Enter enough times to get back to the main prompt. Now, let's define a pin that is connected to our LED:

import machine
pin = machine.Pin(2, machine.Pin.OUT)

Finally, we can create a simple loop that will call the pin every 500 milliseconds using the following code:

import time
while True:
toggle(pin)
time.sleep_ms(500)

Once you have pressed Enter several times, the function will be complete and you will observe that the LED is blinking at the desired frequency. Now that we have verified that MicroPython is fully functional and that we can interface it's hardware successfully, use Ctrl + C to stop the application from executing. Let's now set up our Wi-Fi access.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.104.120