

Uncomment one of the following lines accordingly to the board you’re using. Insert your network credentials in the following variables: ssid = 'REPLACE_WITH_YOUR_SSID' boot.pyĬopy the following code to your boot.py file. In this section, we’ve created a web server example that allows you to control a relay remotely via web server. Sleep(10) Control Relay Module with MicroPython Web Server If you’re using a normally closed configuration, send a LOW signal to stop the current flow. Stop the current flow by sending a HIGH signal to the relay pin. If you’re using a normally closed configuration, send a HIGH signal to light up the lamp. In the while loop, send a LOW signal to light up the lamp for 10 seconds. Comment the previous line and uncomment the following. In case you’re using an ESP8266, use GPIO 5 instead. Then, we define a Pin object called relay on 26 (if you’re using an ESP32) and define it as an output. We also import the sleep() method from the time module to add delays. Import the Pin class from the machine module to interact with the GPIOs. It lights up your lamp for 10 seconds and turn it off for another 10 seconds. The diagram shows wiring for a 2-channel relay module, wiring a different number of channels is similar.Ĭopy the following code to the main.py file and upload it to your board. ESP32 Schematic DiagramĬonnect the relay module to the ESP32 as shown in the following diagram. While programming the ESP or wiring your circuit make sure everything is disconnected from mains voltage.Īlternatively, you can use a 12V power source to control 12V appliances. If you’re not familiar with mains voltage ask someone who is to help you out. Warning: in this example, we’re dealing with mains voltage. Wiring a Relay Module to the ESP32/ESP8266 That configuration physically isolates the relays from the ESP with the module’s built-in optocoupler, which prevents damage to the ESP in case of electrical spikes. Without the jumper cap, you need to provide an independent power source to power up the relay’s electromagnet through the JD-VCC pin. That means the relay electromagnet is directly powered from the ESP power pin, so the relay module and the ESP circuits are not physically isolated from each other. With the jumper cap on, the VCC and JD-VCC pins are connected. Notice that the module has a jumper cap connecting the VCC and JD-VCC pins the one shown here is yellow, but yours may be a different color. The JD-VCC pin powers the electromagnet of the relay. The second set of pins consists of GND, VCC, and JD-VCC pins. Use a normally open configuration when you want the current to flow occasionally (for example, turn on a lamp occasionally). You should use a normally closed configuration when the current should be flowing most of the times, and you only want to stop it occasionally. This means that you’ll have the following scenarios: The relay is triggered when the input goes below about 2V. The signal you send to the IN pins, determines whether the relay is active or not. If you have four channels, you’ll have four IN pins, and so on. If your relay module only has one channel, you’ll have just one IN pin. The first set consists of VCC and GND to power up the module, and input 1 ( IN1) and input 2 ( IN2) to control the bottom and top relays, respectively. The low-voltage side has a set of four pins and a set of three pins. The number of channels determines the number of outputs we’ll be able to control.

You can find relay modules with one, two, four, eight and even sixteen channels. There are different relay modules with a different number of channels. It can be controlled with low voltages, like the 3.3V provided by the ESP32/ESP8266 GPIOs and allows us to control high voltages like 12V, 24V or mains voltage (230V in Europe and 120V in the US). Introducing RelaysĪ relay is an electrically operated switch and like any other switch, it that can be turned on or off, letting the current go through or not. Learn more about MicroPython: MicroPython Programming with ESP32 and ESP8266 eBook. Flash/Upload MicroPython Firmware to ESP32 and ESP8266.Install uP圜raft IDE ( Windows, Mac OS X, Linux).Flashing MicroPython Firmware with esptool.py.Installing and getting started with Thonny IDE.We suggest using Thonny IDE or uP圜raft IDE: You also need an IDE to write and upload the code to your board. To follow this tutorial you need MicroPython firmware installed in your ESP32 or ESP8266 boards. Guide for ESP8266 Relay Module with Arduino IDE – Control AC Appliances + Web Server Example.Guide for ESP32 Relay Module with Arduino IDE – Control AC Appliances + Web Server Example.We have similar guides using Arduino IDE: We’ll take a look at how a relay module works, how to connect the relay to the ESP32 or ESP8266 boards and build a web server to control a relay remotely.
