Friday, August 31, 2018

Driving Relays with the ESP8266

The ESP8266 has a current limit of 12ma, so we have to take care when driving relays.

First I start with a 817C Optoisolator. The datasheet says says the IR LED drops 1.2v, so I plug 3.3v source voltage, 1.2v forward voltage, and 12ma forward current into the calculator at http://led.linear1.org/led.wiz and I get a 180 ohm resistor as the optimum value.

Now, according to the datasheet for the 817C, the Current transfer ratio at 12ma is around 120%, so I can only conduct around 14-15ma through the photo transistor, not nearly enough to trigger a relay.

Let's have the photo transistor trigger an additional transistor, one that can allow the full current needed by the coil of the relay. I have chosen a 2N5551, since the datasheet says it can handle 600ma and 5v.

Now the relay. I chose a HK19F 5v 125ohm relay which, according to Ohm's law and the datasheet, draws 40ma. It's a DPDT relay (most commercial modules use a SPDT), but the contacts are only rated for 1A or so.

Don't forget a flyback diode for that relay. I used a 1N4148, which, according to the datasheet, easily handles the current and voltage of the relay.

If you want, you could add a LED / 150 ohm resistor across the coil of the relay as a visual indicator.

Now a simple

digitalWrite(5, HIGH);

makes it all happen!

UPDATE: 3v MOSFET instead of optoisolator. Simpler and cheaper!



Thursday, August 30, 2018

Sync'ing ESP8266 GPIO Pins to Arduino Pin Numbers

Many ESP8266 12E Boards come with Arduino style pin numbers labeled on the boards, but you use the GPIO pin numbers in your code. If you include the following lines of code in your sketch before setup(), you can use the number silkscreened on your board (different boards may have different pin matchings).

const byte D0 = 16;
const byte D1 = 5;
const byte D2 = 4;
const byte D3 = 0;
const byte D4 = 2;
const byte D5 = 14;
const byte D6 = 12;
const byte D7 = 13;
const byte D8 = 15;
const byte D9 = 3;
const byte D10 = 1;

Now you can use something like

digitalWrite(D6, HIGH); //uses D6 on the ESP8266

in your sketch.

Of the above GPIO numbers, not all of them are easily available for use. Some are used for flash memory (GPIO 6 - 11), others are used for boot and flash purposes (GPIO 0, 2 & 15). GPIO 0-15 all have a built-in pull-up resistor (use INPUT_PULLUP), just like in an Arduino, while GPIO16 has a built-in pull-down resistor. The safe ones are GPIO 1, 3-5, 12-14, and 16. All GPIO pins can source about 12ma, and sink close to 20ma.

The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only". This means that the variable can be used just as any other variable of its type, but its value cannot be changed. You will get a compiler error if you try to assign a value to a const variable (which is a good thing). Unlike #define, it types the "variable" instead of making the compiler guess. For these reasons it's preferable to use const instead of #define (https://www.arduino.cc/reference/en/language/structure/further-syntax/define/).

A byte stores an 8-bit unsigned number, from 0 to 255. This takes less space than an int, which takes up two bytes. You may see some code samples use uint8_t instead of byte. It means the same thing.

Further reading:

https://tttapa.github.io/ESP8266/Chap01%20-%20ESP8266.html

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/powering-the-esp-12e-nodemcu-development-board/

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-esp8266-lolin-nodemcu-getting-started/

http://esp8266.github.io/Arduino/versions/2.0.0/doc/reference.html





Related Posts Plugin for WordPress, Blogger...