Esp8266 Team Hack Leds
by querry43 in Circuits > Microcontrollers
562 Views, 1 Favorites, 0 Comments
Esp8266 Team Hack Leds
This project is part of a series for an instructables team hack. See the series for more information.
The esp8266 dev board has an led on it. Let's make it blink!
Start by importing a library and using it to define an led on pin 2. (Some dev boards may use different pins, or may not have an led at all. You may need to play around.)
from machine import Pin led = Pin(2, Pin.OUT)
Now toggle the led on and off with these two lines.
led.on() led.off()
Blink!
Wee! Now let's make it blink on its own.
First a note about the python language. Python really likes whitespace (spaces, newlines). When you see examples with spaces at the beginning of the line, keep those spaces! Python will not care if they are spaces or tabs. It will not care how many spaces or tabs you use. But, it does care if you are consistent. If one indented line has 4 spaces, then the other indented lines should also have 4 spaces.
Create a forever loop turning the led on and off. Remember, use consistent indentation!
# blink.py from machine import Pin from utime import sleep_ms led = Pin(2, Pin.OUT) while True: led.on() sleep_ms(500) led.off() sleep_ms(500)
If you are in the webrepl, control-c will make the program stop. You can also use the reset button on your esp8266 dev board, or unplug and re-plug the board.
Moar Colors
The team hack kits include an RGB LED. We're going to connect it up and make some more colors.
The RGB LED actually has three separate chips inside for each of the primary colors: red, green, and blue. It is `common cathode`, meaning that each color has a separate positive voltage input but share the same voltage output.
The kit part is actually four components: the red, green, and blue legs have resistors. You may be able to see them as a spool shape under the blue heat shrink tubing. These resistors limit how much current flows through the LED and without them the LED would burn out. See Choosing-The-Resistor-To-Use-With-LEDs for more info.
Connect up the LED follow the images and the mapping below.
Red -> D1 Green -> D2 Blue -> D3 Black -> GND
Now define pins for each color of the RGB LED. Note that the pin names in the code will not match the labels on the board. This is something you will get used to if you work with microcontrollers. Oh well.
# init_rgb.py from machine import Pin red = Pin(5, Pin.OUT) green = Pin(4, Pin.OUT) blue = Pin(0, Pin.OUT)
Play with the colors. Can you make purple?
red.on() blue.off() green.on()
Next Steps
So what's next? Here we control one individual LED, but we could also control many to make light effects. Here are some project ideas.