Switch Tutorial Part 1 - Turn on and off a Relay
In this section we’ll walk through creating a Sinric Pro Switch for ESP32, ESP8266 or Raspberry Pi Zero W and then control via Alexa, Google Home or SmartThings.
Prerequisites :
- ESP32, ESP8266 or RaspPi W x 1.
- SPDT Relay controller x 1.
- Jumper Wires.
Quick introduction to Relay
A relay is an electrically operated switch that can be turned on or off by a low-voltage signal. It is like a regular switch in that it allows current to flow through or not, but it can be controlled by a much smaller voltage. This makes relays ideal for MCUs where it is necessary to control a high-voltage circuit with a low-voltage signal.
How it works
The operation of a relay can vary depending on the manufacturer, the wiring, and the type of relay controller being used. Some relay controllers require a high-level signal from the MCU to activate the relay, while others require a low-level signal. It is important to check the specifications of the relay controller before using it to ensure that it is compatible with your MCU.
High-level signal: A high-level signal is a signal that is above a certain voltage threshold. For example, a 5V relay controller may require a signal that is above 3.3V to activate the relay.
digitalWrite(pin, HIGH);
Low-level signal: A low-level signal is a signal that is below a certain voltage threshold. For example, a 5V relay controller may require a signal that is below 0.8V to activate the relay.
digitalWrite(pin, LOW);
High Voltage Connectors
The relay module in the above has a single connector with three sockets: Common (COM), Normally Closed (NC), and Normally Open (NO).
Common (COM): COM is the common connection for both the NO and NC pins. Connect the high voltage device to the COM pin.
Normally Closed (NC): The circuit is closed by default.
Normally Open (NO): The circuit is open by default.
Normally open (NO) and normally closed (NC) contacts allow you to use one pin to control two separate circuits depending on your requirements. For example, you could use one pin to control the power to a light bulb and another pin to control the power to a motor. When the pin is high/low the light bulb will turn on otherwise the motor will turn on.
Wiring
MCU | Pin |
---|---|
ESP32 | 16 |
ESP8266 | 12 (D6) |
RaspPi W | 6 |
Before we integrate with Sinric Pro, it is important to verify that the relay is wired correctly. You can use the following code to check whether the relay turns on and off every 5 seconds.
Step 1 : Create a new device in Sinric Pro
- Login to your Sinric Pro account, go to Devices menu on your left and click Add Device button (On top left).
- Enter the device name Switch, description My First Switch and select the device type as Switch.
- Click Next the in the Notifications tab
To receive push notifications when your device goes online or offline, enable the “Connect and Disconnect” option in the Sinric Pro app. To receive push notifications when your device turns on or off, enable the “On and Off” option.
-
Click Others and Click Save
-
Next screen will show the credentials required to connect the device you just created.
- Copy the Device Id, App Key and App Secret Keep these values secure. DO NOT SHARE THEM ON PUBLIC FORUMS !
Step 2 : Coding
2.1 Install Sinric Pro Library
You can generate the code using Zero Code feature or write it by your self. If you do not have programming experice, we recommend to use Zero Code feature in the Portal to generate the code, download and flash.
2.2 Complete Code
Now you should be able to control the relay via Portal like in the below video.
Step 3 : Link Sinric Pro Alexa Skill with your account.
• Open your Amazon Alexa app. • Goto Skills & Games. • Search for Sinric Pro. • Click ENABLE TO USE. • Enter your Sinric Pro credentails in linking page. • Ask Alexa to discover new devices. • Ask Alexa to turn on the Switch |
Step 4 : Link Sinric Pro Google Home action with your account.
• Open Google Home and click on Add. • Select Setup Device • Select Works with Google Home • Search for Sinric Pro. • Link your account. • Discover new devices • Ask Google Home to turn on the Switch |
Step 5 : Link Sinric Pro SmartThings with your account.
• Open the SmartThings app and tap the + Icon • Select Device • Select Sinric Pro under By Brand • Link your account. • Now you can turn on or off Switch from the dashboard |
Continue to Part 2 of this article series to learn how to add a push button to toggle the relay.
Troubleshooting
Please refer to our Troubleshooting page for possible solutions to your issue.
This document is open source. See a typo? Please create an issue