Happy Arduino Day 2016 + Testing a Power Relay

Happy Arduino/Genuino Day 2016!

arduino uno pic

Take some time today to do a little project with your Arduino!

Testing a Power Relay

My tech savvy grandma recently got me a very nice IoT power relay! I spent a while testing it out by making a simple system to automatically turn my desk light and my Pi monitor on or off. I hooked an SR-04 ultrasonic sensor to my Arduino Uno and wrote a sketch to have it detect when my hulking mass is close to my desk and send a signal to turn the relay on or off.

power relay hooked up
This is my power relay setup connected to an Arduino and ultrasonic sensor. I taped the ultrasonic sensor down to keep it from wiggling about. The Arduino is being powered by a USB port on my Pi 2.

The most challenging part of creating this little system was getting accurate readings from the ultrasonic sensor. They can read inaccurately if objects are moving around in front of the sensor, and the light would flicker annoyingly if I moved too quickly. I figured out how to smooth the value by reading from the sensor 30 times in 1.5 seconds, and then taking the average of all 30 values.

The power relay works great. It required no setup out of box, and it’s simple to connect your controller. Its only disadvantage is that it doesn’t have an ‘always-on’ power source, which requires you to have an external power source for the device controlling the relay.

iot power relay
This is the power relay I tested.

Check out the sketch I wrote for this project below, or click here to download.


Using an Arduino with a Power Relay

Parts used:
--ultrasonic sensor
--desk lamp
--Arduino Uno
--120v relay

This sketch uses an ultrasonic sensor to detect whether
a person in near or sitting at a desk, and sends a 
signal to a power relay to turn desk lighting on or off.


#include <NewPing.h>

const int LIGHT = 10;
const int TRIGGER = 12;
const int ECHO = 11;

NewPing sonar(TRIGGER, ECHO);

void setup(){
  pinMode(LIGHT, OUTPUT); 

/* The main loop iterates every 2.5 seconds. */

void loop(){
  /*In order to get a accurate measure of whether
  someone is near, we read the ultrasonic sensor
  30 times and take the average. That way, if the
  ultrasonic sensor reads sporadically as it 
  occasionally does, those values will be averaged out.  
  This takes 0.05 * 30 = 1.5 seconds to get a value
  from the sensor. */
  while (int i, i < 30, i++){
    sum = sum + sonar.ping_cm();
  int average_distance = sum/30; 
  /* Using the value, we send a signal to the relay
  depending on whether someone is detected less than 
  130 cm away. We also pause for a second for good 
  measure. */
  if (average_distance < 130){
    digitalWrite(LIGHT, HIGH);
    digitalWrite(LIGHT, LOW);

How to Use Ultrasonic Sensors with the Arduino

I’ve made my first video tutorial! I obtained some SR-04 ultrasonic sensors quite cheaply, and I’ve been learning how to use them with the Arduino, with good results. I’ve created a tutorial covering how to get the proper libraries and how to use the basic functions in the Arduino IDE to interface with the sensor.

Here’s the steps (covered in the video) on how to get NewPing, a library created for controlling ultrasonic sensors, up and running with the Arduino IDE:

  1. Use this link to download the NewPing Library: https://bitbucket.org/teckel12/arduino-new-ping/downloads.
  2. Click “Download NewPink v1.7” and save the .zip file.
  3. Open the Arduino IDE, and go to ‘Sketch>Include Library>Add .ZIP Library’ and then navigate to where you saved the .zip download, and click ‘open.’
  4. Go to ‘Sketch>Include Library’ again and scroll all the way down to the bottom and select NewPing.
  5. The NewPing library will now be included in your sketch.
  6. For some sample sketches and documentation of NewPing, look here: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home#!syntax.

For code sample written in the tutorial, click here.

P.S. – Yesterday was the first anniversary of this blog (this is my 48th post)! 😀

ultrasonic sensor schematic

Candle Snuffer with Arduino and 9V Battery

another side view of snuffer

Having the urge to make a ‘pointless invention’,  I made a candle snuffer from a DC motor off an old helicopter, a 9V battery, and Erector set pieces. Basically, a fan blade attached to the motor and wired to a pushbutton sends a sufficient gust of air to blow out the flame of a small candle. I made two different versions of the snuffer, one with a only a 9V battery and one with an Arduino powered of a 9V battery, with the motor connected to 9V.

The snuffer basically has a little tray for a candle, with a fan pointed down at the top of the candle. A breadboard is mounted upright on the side, and the 9V battery is stowed under the tray. Below is a view of the whole thing lying on it’s side without the candlelaying flat

Arduino Micro Candle Snuffer

When I first got the idea for a candle snuffer, I immediately thought using the Arduino Micro with a fan and allowing for time delays, temperature sensing for ‘overheat’ shutoff, and a photoresistor to detect if the candle is lit or not. The first thing I made was the breadboard mount and the candle tray out of Erector set pieces, and then I attempted to add the photoresistor, near the candle. However the heat from the candle would have likely damaged the photoresistor.

Getting the motor to spin fast enough to blow out the candle flame was a challenge with Arduino due to power constraints and transistor problems. I first tried direct drive off an Arduino pin’s 5V, but that did not make the motor spin nearly fast enough. Then I tried driving the motor with 9V via PNP transistor controlled by the Arduino, which, after a couple transistor overheats, still did not allow the motor to spin fast enough maybe due to voltage drops and such. My final version uses a 5V relay which seems to work really well. The fan spins more than fast enough using the relay. I’m not a huge fan (no pun intended) of relays because they can be difficult to wire and take up a lot of space on the breadboard. I had to tape my relay in place with my bright yellow electrical tape to keep it from popping out.arduino_candle snuffer wiring

Because of all the frustration with driving the tiny little fan motor, I didn’t do much with the Arduino software side of things. You can check out the code I used here, which basically sends a gust of air at the candle flame when the button is held down. I didn’t add a time-delay or other sensing capabilities yet, though I may not because this is kind of already a benched project.

In my wiring, the 9V battery is connected to the Arduino’s Vin pin, and its ground is tethered to the Arduino’s ground. The relay turns on and off when it gets a signal from the Arduino. The fan runs off of the Arduino’s constant 5V pin. I’m not sure how this differs from direct motor drive from a pin, but the fan spins moves much faster.

Candle Snuffer Powered Off of 9V

Halfway through building the Arduino Micro version, I tore it all up and resorted to a pushbutton wired between 9V and the motor. When you press the button, the fan comes on and blows out the candle. I did look into using a 555 timer IC to try and create a 30 minute time delay until the candle is snuffed, but that ended up being a little complicated since I don’t have all the capacitor values. Ironically, this version of the candle snuffer does exactly the same thing as the Arduino Micro version.

The schematic is below. It’s quite simple.

9v circuitpic candle snuffer
side view of snuffer

Oscillating Fan with Arduino – Part 2

Oscillating fan with micro the whle thingI’ve finished up my oscillating fan! My final design changed significantly though due to some technical difficulties. In the end I opted to use the Arduino Micro because it was the right size for this smaller project. Unable to obtain a 9v battery, I used 4 AA batteries, which barely power the Arduino and the fan. I was originally was going to use a temperature sensor with temperature indicator LEDs, but my temperature sensor was behave erratically. Sadly, I may have fried it at some point. Instead, I used two potentiometers, one controlling the fan speed, and one controlling the oscillation speed, and they work quite nicely. Using Erector set pieces, I made a tray for the breadboad it taped to the battery pack. I used more pieces to mount the fan and support the breadboard, and the final project is nice and compact.

Oscillating fan with micro side view

To view code, click here.diagram of the Oscillating fanIf you build a project like this, with external power, you have to be careful of which rail has which power connection. In this project, I had one rail on 6v (the battery) and one on 5v (the Arduino’s logic voltage). If you draw from the 6v to power the potentiometers or other sensors, you could damage your Arduino. It’s also important to remember to connect the Arduino’s and the batteries’ ground together.

Below is the fan mounted to the servo using Erector set pieces.

Oscillating fan with micro fan construct

Oscillating Fan With Arduino – Part 1

Having made a simple air conditioning system with the Arduino Uno a few months ago, I’ve decided to upgrade that project by adding a servo to the fan. I spent a little time yesterday to build an assembly out of Erector set pieces for the 2 inch computer fan and standard servo. I created a primitive prototype with a potentiometer that can control the position of the servo, thus making the fan ‘oscillate.’

oscillating fan with arduino

Now that I have most of the mechanics figured out, I plan on adding an indicator LED, temperature sensor, and a button to toggle between turning on and off the oscillating. I’ll keep the potentiometer to control turn on the threshold temperature for the fan. I will also probably add an external power supply; the Arduino’s 5v doesn’t cut it for the maximum fan speed. It’ll be a nice little desk fan once I’m finished!

Arduino code for the final version will be posted in Part 2!

Arduino Air Conditioning

The first ‘big’ project I tackled with my Arduino is a simple air conditioning system. Using an old computer fan and a TMP36 temperature sensor, I programmed the fan to turn on whenever the temperature gets to high. The Arduino’s 5v wasn’t enough power for the fan to spin effectively, so I used a transistor and a 9v battery instead of the digital output directly.

After getting the fan to work, I added an indication LED as well as potentiometer thermostat, which in my code sets the temperature between 50-100 degrees F. The LED turns on whenever the fan is on.

Arduino Air Conditioning

Here’s the code. Just copy and paste it into the Arduino IDE. Make any improvements that you like on it.

Archaic Air Conditioning Code

Build the circuit using the following schematic. You don’t have to add the LED or potentiometer, or even the 9v battery and transistor. Depending on your fan type you might be able to run the fan with the Arduino’s 5 volts.

Arduino Air Conditioning_Fritzing Schematic