> [!TLDR] Using relay module with ESP32 > This guide explains how to control a single channel relay module with an ESP32 microcontroller. Example code demonstrates using `pinMode()` to set the GPIO pin as an output and `digitalWrite()` to toggle the pin HIGH (on) and LOW (off), controlling the relay's state. # Introduction Using the [[Módulo relé de canal único]] with an [[ESP32]] involves controlling GPIO pins (General Purpose Input/Output). Here's a breakdown of how to do it! ## Requisites - Hardware - [[ESP32]] - [ESP32](app://obsidian.md/ESP32) is a series of low cost and low power consumption microcontrollers. It is also a system-in-one-chip with integrated microcontroller, Wi-Fi and Bluetooth - [[Módulo relé de canal único]] - The relay is an electromechanical device that uses an electric current to open or close the contacts of a key. - [[Diodo emissor de luz|LED]] - Used to show the on and off status of the [[Módulo relé de canal único|Relay module]]. - [[Resistores]] - Used to limit the current thought the [[Diodo emissor de luz|LED]]. - Software - [[Arduino IDE]] or [[Wokwi]] for programming/simulating the code to run on the [[ESP32]]. - Make sure to [[Instalando o driver da ESP32 no Windows|install the ESP32 driver]] to be able to upload the code to the [[ESP32]]. # Hardware Connections * **[[Módulo relé de canal único]]:** * **Pin 1:** Connect to the VCC on the [[ESP32]] * **Pin2:** Connect to a GND/Ground of the [[ESP32]] * **Pin3:** Connect to GPIO digital pin of the [[ESP32]] ![[Relay module with ESP32.png]] # Example Code 1 - Using the `DHT sensor library for ESPx` ```cpp void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println("Hello, ESP32!"); pinMode(15, OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(15, HIGH); delay(1000); digitalWrite(15, LOW); delay(1000); delay(10); // this speeds up the simulation } ``` **Explanation:** * `pinMode(15, OUTPUT);`: This line configures GPIO pin 15 (a digital pin on the ESP32) as an output. This means the ESP32 can control the voltage level on this pin. * `digitalWrite(15, HIGH);`: This line sets the voltage on pin 15 to HIGH, effectively turning it on. * `digitalWrite(15, LOW);`: This line sets the voltage on pin 15 to LOW, effectively turning it off.