Twitter icon
Facebook icon
LinkedIn icon
Google icon
Reddit icon
StumbleUpon icon
Del.icio.us icon

Internet-Controlled Light Switch

Added to IoTplaybook or last updated on: 08/01/2022
Internet-Controlled Light Switch

Story

I decided to make an Internet-connected light that I could also control physically. Did I leave the lights on? I needed a quick way of knowing and also turning the lights on or off.

Please note that this project uses alternating current and you will need to be extremely careful so you don't get electrocuted. Even touching the relay while it is powered could send a jolt through you. Please be careful.

I kept this simple by using the Arduino Nano RP2040 Connect. This microcontroller has an integrated WiFi radio so I can connect it to the internet.

Things used in this project

Hardware components

Arduino Nano RP2040 Connect
Arduino Nano RP2040 Connect
 
× 1

Arduino.dd

CPC

Farnell

Newark

Grove - Relay
Seeed Studio Grove - Relay
 
× 1

Seeed Stuido

Grove - 4 pin Male Jumper to Grove 4 pin Conversion Cable
Seeed Studio Grove - 4 pin Male Jumper to Grove 4 pin Conversion Cable
 
× 1 Seeed Studio
Breadboard (generic)
Breadboard (generic)
 
× 1

Newark

SparkFun

Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
 
× 1

Newark

Lamp Holder, E27/ES
Lamp Holder, E27/ES
 
× 1

Newark

Incandescent Lamp, 230 V
Incandescent Lamp, 230 V
 
× 1

Newark

Mains Power Cord, With 13A Fuse
Mains Power Cord, With 13A Fuse
 
× 1

Newark

Software apps and online services

Arduino IoT Cloud
Arduino IoT Cloud
 
  Arduino

Hand tools and fabrication machines

Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
 
 

Newark

Multitool, Screwdriver
Multitool, Screwdriver
 
 

Newark

Step 1: Planning

I wanted a lamp that I could control both physically and remotely. In order to satisfy that requirement, the physical switch would need to be a toggle: one press to turn it on and another to turn it off. That way the lamp could be turned on or off either physically or remotely.

In order to control alternating current (AC) from a microcontroller, I had to introduce a relay. When you pick a relay you need to check the rating for the type of current and voltage that you need to support. The relay I went with supports a peak voltage of 250V at 10A. This worked for me because I was only powering a light bulb.

I chose the Arduino Nano RP2040 Connect because it has a WiFi radio and is supported by the Arduino IoT Cloud. If it wasn't supported I would need to write extra code for the cloud layer, and I wanted to avoid that at this stage.

Grove components normally utilize a Grove shield. I got a male-pin to Grove converter so I can use the relay with a breadboard.

Step 2: The Lamp

My advice for getting started is to build the AC component first. Take your mains cord and snip off the end that doesn't connect to the wall. Next, strip off some of the insulation on the wires and attach these to the lamp holder. Be careful so that you don't leave any exposed wires. Finally, screw in your lamp and plug it in. The lamp should turn on. If it doesn't, please debug it at this point. You will need to remove it from the wall to turn it off.

Step 3: The DC Components

The assembly of the DC components

The assembly of the DC components

I introduced a battery into the mix so I can operate the system without using USB power. To use a battery you will need to regulate the voltage to 5V or 3.3V otherwise you will destroy the microcontroller. This part is completely optional.

One leg of the button is connected to D10, with the other leg connected to GND. This is connected as a digital input with a pull-up resistor.

The relay is connected to VCC and GND as indicated by the colors of the cables. The yellow cable is connected to D2. This is treated as a digital output.

Step 4: Offline Code

I always write my code to work offline before I introduce any networking components. This helps with any debugging. Tactile buttons can be difficult to work with when you want to toggle the state of a variable. This can be difficult to debug. The approach I took was to introduce debouncing. The code is shown below.

#include <Bounce2.h>

const int pin = 2;
const int button = 10;
bool led_state = false;

Bounce bouncer = Bounce();

// the setup function runs once when you press reset or power the board
void setup() {
    pinMode(pin, OUTPUT);
    pinMode(button, INPUT_PULLUP);
    bouncer.attach(button);
}

// the loop function runs over and over again forever
void loop() {
    bouncer.update();
    if (bouncer.changed() && bouncer.read() == LOW) {
        led_state = !led_state;
        Serial.println(led_state);
    }
    digitalWrite(pin, led_state);
}

If everything works as it should the relay will turn on and off when you push the tactile button.

Step 5: Cloud Setup

If you don't have an account on the Arduino IoT Cloud, please go ahead and create one.

Next, go to the Devices tab and register your microcontroller. This could take up to five minutes so please be patient.

Device setup on Arduino Cloud

Device setup on Arduino Cloud

Step 6: Set Up a Thing

This step is actually three different activities. Think of a Thing as the complete unit that contains your microcontroller, in this case, your IoT device. Our Thing is a lamp, so you can call it that.

Set up a Thing

Set up a Thing

You will need to configure three different components:

  • Variables: these are the things that change. In this case, it's whether our light is on or off. I called the variable lights and gave it a boolean type. I also made it read & write so both the device and the cloud environment can change the value.

Define your variable

Define your variable

  • Associate a device. This step lets the platform know what microcontroller to read and write these variables.
  • Network: This configures the device so that it has access to the internet.

Step 7: Program the Device

With the above configuration complete, click on the Sketch tab.

Edit the Sketch

Edit the Sketch

Unlike the Arduino IDE, the sketch will not be empty. The setup() and loop() functions will have some code in them. Be sure to leave them in there.

You can scroll to the bottom of the sketch to see an event handler for when the lights variable changes on the cloud. This is useful for doing something but I don't use it here because I handle change in the loop() function.

The event handler

The event handler

You will need to update the sketch so that it recognizes the pin and relay and does the right thing when the button is pressed. This is shown in the code snippet below.

You can verify the code and deploy it to the device from this interface. There is also a more full-fledged editor that shows you other files that you can't see at the moment. For example, not that this sketch doesn't show you how the WiFi connection is established or what the credentials are.

Go ahead and test that this works as expected.

Step 8: Connect the lamp to the relay

  • Disconnect both the lamp and the Arduino boards from power.
  • Splice any part of the cable on the lamp.
  • Cut the neutral wire and strip two ends.
  • Connect both ends to the relay.
  • Connect the Arduino and check that it works and the relays lights up as you toggle.
  • Connect the lamp to power and try the whole system.

Connect the lamp to the relay

Connect the lamp to the relay

Step 9: Add a Dashboard

Dashboards give you a way to monitor and control your variables. Arduino IoT Cloud also has a mobile application that gives you access to your dashboards. Follow the steps below to set up a dashboard.

  • Navigate to the Dashboard Tab and Click on the Create button.
  • Give your new dashboard a name.
  • Click on the Add button and choose an appropriate widget for your variable. In this case, I chose a toggle.
  • Link the widget to a variable. You can add as many widgets as you would like to a dashboard.

Once you set your dashboard to viewing mode (the eye besides the pencil, you can use the widget to control your lights!

Congratulations.

Schematics - Circuit Board Diagram

Use this layout to see how to connect your components

Code - Remote Lights

Use these files as a reference to build your Arduino IoT Cloud remote lights using a Seeed Studios Grove Relay and an Arduino Nano RP2040 Connect

securetorobert / remote_lights

An Arduino IoT Cloud project for remotely controlling lights using a Seeed Studios Grove Relay and an Arduino Nano RP2040 Connect. — Read More

Download as zip

Credits

Robert John

Robert John

1 project • 4 followers

Data Team lead by day, community and hardware builder at night. Arm Innovator, Edge Impulse Expert, Google Dev Expert.

 

Hackster.io

This content is provided by our content partner Hackster.io, an Avnet developer community for learning, programming, and building hardware. Visit them online for more great content like this.

This article was originally published at Hackster.io. It was added to IoTplaybook or last modified on 08/01/2022.