Simplify Your Life: Using a Relay Module

Simple relay

A Relay

Today’s blog is on a simple topic, but one that comes up a lot, and  may be of use, especially to beginners. Many projects involve controlling a higher voltage device or devices based on an external event triggered by a sensor, such as a PIR. And to control the higher voltage device, a relay is used. Along with the relay, you’ll need a transistor to provide adequate current to drive the relay, a diode to protect from reverse currents when the relay switches off, and probably a resistor to limit the current draw. This is simple enough, but another option is to use a relay module. The module combines a relay along with these other components into one simple package.

Depending upon your application, you may also put some sort of timer circuit, or a microcontroller such as an Arduino, or a single board computer (SBC) like a Raspberry Pi between the sensor output and the relay input. This lets you implement more complex logic than simply having the relay echo the output from the sensor. For example, one could have a light toggle on or off each time the sensor is triggered. Or, when triggered, one could have a device stay on for a set amount of time before turning off. Or even  more complex actions. But if you don’t need the added logic, you can use the sensor directly, without a microcontroller or SBC.

The output voltage from the sensor or controller board must be high enough to trigger the relay, but voltage alone is not enough. The driving device must also be capable of supplying enough current. This is often not the case for either sensors or computer boards. They lack the power to drive many relays.

Relay and driver circuit, showing transistor, diode, and resistor

Relay and Driver Circuit

The solution is to use a transistor. The sensor or board switches on and off the larger current flow through the transistor, and this, in turn, drives the relay. In addition, one needs a resistor to limit the current drawn from the sensor or controller and a “flyback” diode to protect both devices and the transistor from reverse currents when the relay turns off. This is shown in the figure, taken from Circuit Digest’s Arduino Relay Control Tutorial.

You can learn more about a relay and the relay driver circuit in the article How Electrical Relays Work at Circuit Basics.

 

Relay Module Board

Relay Module

But rather than using discrete components, there’s another option which is often quicker and easier: the relay module. A relay module combines all of these elements into a single board. It may include additional features, such as an LED to indicate when the relay is triggered. In this way, you only need your sensor, optionally a controller for more complex logic, and the relay module. No discreet diodes, transistors, or resistors required. I’m using the one discussed in this article, along with a PIR sensor, to trigger the “Try Me” switch on a Halloween prop. Although the signal output for the PIR is 3.3V and the relay is a 5V input signal relay, it seems to work just fine. As you can see, it’s the same relay, mounted onto a circuit board along with the transistor, resistor, diode, and indicator LED. In addition, the signal side inputs are connected to 3 male pins for easier connections. The pins are spaced so that they will fit into a standard breadboard or for connecting a standard 3-wire servo cable.

So the next time you need to trigger a higher voltage AC or DC device, reach for a relay module.

Pocket Protectors: They Don’t Just Protect Pockets Anymore

Overview

This is just a quickly put together project to have a little fun with social distancing. The project uses an ultrasonic distance sensor, microcontroller, and an RGB LED. The set-up is hot glued to a piece of cardboard, with a battery pack glued to the folded over part that fits in a shirt pocket. If it doesn’t detect anything within 10 feet, the LED is off. If it detects something between 8 and 10 feet, it lights up green, indicating a proper social distance is being maintained. Between 8 and 8 feet, it turns yellow, and if it detects something 6 feet or less away, it flashes red to warn both parties that the proper social distance is not being maintained.

As I mentioned, it’s just for fun, and isn’t very practical, as the sensor reacts to anything in front of it, not just another person. If you really want to get fancy, you could add a camera and face detection, so that the sensor only reacts when a person is in front of you.

Hardware

Social Distancing Sensor

Social Distancing Sensor

RCWL-1601 Ultrasonic Sensor

breadboard closeup

A closeup of the fully wired breadboard and components

  • The system uses an Adafruit RCWL-1601 distance sensor. A nice thing about this sensor is that it can operate at either 3V or 5V. The sensor has four pins: Vin, Ground, Trigger, and Echo. You send a trigger pulse to make a measurement, and then the length of the return pulse on the Echo line tells you the distance.
  • The microcontroller is a Teensy 2.0 that I had lying around.  Pretty much any Arduino compatible board will work.
  • The battery input is run through a 7805 voltage regulator to put our a steady 5V. I should be providing a bit higher voltage as input, but the 6V does seem to work.
  • The RGB LED is one I had around, I don’t remember the model.
pocket protector social distance sensor

The finished Social Distance Sensor

Everything is wired up using a breadboard and then the components hot-glued to the cardboard “pocket protector.”

The Software

The software is very straight-forward. The one minor twist is that I decided that when the sensor detected something 6 feet or less away, it would flash, not just have a steady light. Because it also needed to be continually sensing, I couldn’t use delay() to do this. Normally one uses millis() and continually loop and checking on each pass whether or not enough time had passed to turn the LED on or off. In this case, however, I decided to measure distance four times a second (every 250 milliseconds) and also have the duration of each on or off period also be 250 milliseconds. This makes the already simple code a little simpler, but at the expense of flexibility.

The software can be downloaded from github.

The Test: Don’t Stand So Close to Me

So here’s the result. My wife walked towards me holding the camera. As you can see in the video, the LED is initially off. When she gets within 10 feet, it turns green. It skipped over the narrow range when it’s yellow, but when she gets to 6 feet, it begins to flash red.

National Maker Faire 2016

I attended the National Maker Faire held over Father’s Day weekend at the University of the District of Columbia, and thought I’d share some observations and pictures. They’ve added the National Maker Faire to the list of flagship faires around the US, along with the original Bay Area and World Maker Faires. I’ve been to the World Maker Faire once, and from that and what I’ve heard of the Bay Area Faire, the National Faire is much smaller than either. You could certainly see it all in less than half a day. It’s also a bit random in its layout, as it makes use of several areas of campus buildings and grounds. Unlike the other two major faires, I would not travel from out of town to see this one. But for anyone on the greater DC area, I found it well worth my time.

It’s not new, but I hadn’t seen the Intel Arduino 101 board before. I’d have loved if it had WiFi but the built in real-time clock, bluetooth, accelerometer, and gyro make it quite interesting. Apparently the Curie system on a chip module it’s build around also has a 128-node neural network for machine-learning, but there is, at least as of now, no software released to access it.

Ability3D had a table promoting their planned 3d metal printer, with a kickstarter campaign planned for January 2017. They were showing a development prototype model (the final consumer product to be smaller). It prints with powdered metal. I believe they said they were targeting the several thousand dollar range for price, so not inexpensive for a home printer, but an order of magnitude cheaper than current metal powder printers.
Nova Labs, one of the local area makerspaces, was also there with some interesting demos and projects.

There was also a lock picking village put on by TOOOL, which was quite full when I went by. It was also nice to see a short waiting line for the “Learn to Solder” hands on booth. How often do you see people lining up to learn to solder? Unfortunately for some reason the drones only were being flown on Saturday, and there were no drone demos when I went on Sunday.

Below are a number of photos from the event:

Learn to Solder hands on exhibit

Learn to Solder hands on exhibit

Ability3D's booth. Developing a home metal powder 3D printer

Ability3D’s booth. Developing a home metal powder 3D printer

Cardboard pinball kits, with options to add arduino, electronics, servo, etc. There's going to be an upcoming kickstarter campaign.

Cardboard pinball kits, with options to add arduino, electronics, servo, etc. There’s going to be an upcoming kickstarter campaign.