Arduino 120V AC Relay Example

I got tired of poking around with LCD drivers with my Arduino. Time for a quick project to mix it up…staring at a surge protector always puts 120V on my mind. As it turns out, I tore apart an old humidifier a while ago on my ‘workbench.’ Monday night is Katie’s reality TV night…time for something sweet.

Investigating duty cycles on this plastic fan:

fan hack
fan hack

I figured that I could horse around with my PowerSwitch Tail, and make it somewhat mimic a PWM 5V setup. I was curious how long I would have to ‘pulse’ the switch with juice to keep the fan constantly rolling. I started with it fully on, and kicked my ‘active’ duty cycles lower until I reached a nearly-continual state of motion. Pause for Arduino code snippet:

/*
PowerSwitch Tail Template

120V AC Driver w/ LED indicator

Joseph Swanson
2011 | https://swantron.com
*/

void setup() {
// declare pins (13, 7) for writing
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
}

void loop() {
// Fire relay / LED
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);

// Configure for “on” time
delay (50);

// Kill relay / LED
digitalWrite(13, LOW);
digitalWrite(7, LOW);

// Configure for “off” time
delay (950);
}

Pause for a small small-video break:

As that code and vid indicate, 1/20th of a second is all it took to keep the fan rolling, with 19/20th of a second idle. Not bad. Not sure what the takeaway is, but that is something to mention.

Put that knowledge somewhere safe, provided the question of humidifier fan duty cycles should spring up.

DIY Minority Report

Spoiler1: This is awesome.
Spoiler2: I’ve never seen Minority Report.

I do know that there is some sort of hands free interface, and that is what I have put together.

minority report
+1 dizzy

Long story short, I have extended upon my PING))) project to include some sweet touchless home automation. I have the ultrasonic sensor interfacing with my garage door and a lamp, utilizing a servo and a PowerSwitch Tail, respectively.

Hit the bump for an awesome video of this thing in action, and for my spippet.

Continue reading “DIY Minority Report”

Arduino Solar Cell Night Light Concept

So, I’ve formalized the solar cell project I have been poking at for a while. I managed to clean up my code and mess with some initial conditions, etc., and now have a fairly solid proof of concept for a solar cell-centered night light.

lighty
you may want to ramp up that LED a bit

As was the case in my first few runs, my sketch incorporates a five second initialization phase. This acts to set both relative minimum and maximum values which act to provide “full light on” and “full light off” values, respectively. The generated power from the solar cell is read in to the Arduino via analog input, and the LEDs are driven via digital outs. The rest is some simple math that transforms the range of the analog signal into a digital range of zero to two hundo fifty five.

It’s giant-ass-text-having snippet time!

// Solar LED IO
// Joseph Swanson | https://swantron.com
// 2011

// Define constants
const int sensorPin = A3; // Solar cell Pin
const int ledPin = 5; // varuiable LED Pin

// Define variables
int sensorValue = 0; // wipe read value
int sensorMin = 0; // set initial min
int sensorMax = 1023; // set initial max

void setup() {

// turn on Pin 11 LED…indicates calibration period begin
pinMode(11, OUTPUT);
digitalWrite(11, HIGH);

// stay lit for five seconds
while (millis() < 5000) { sensorValue = analogRead(sensorPin); // adjust for real max if (sensorValue < sensorMax) { sensorMax = sensorValue; } // adjust for real min if (sensorValue > sensorMin) {
sensorMin = sensorValue;
}
}

// end Pin 11… calibration period finito
digitalWrite(11, LOW);
}

void loop() {
// read the solar cell analog
sensorValue = analogRead(sensorPin);

// apply a little calibration to the sensor reading
// bit from example sketch at http://arduino.cc
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

// set constraint for outliers with respect to min/max
sensorValue = constrain(sensorValue, 0, 255);

// fade the LED from one to 255
analogWrite(ledPin, sensorValue);
}

Pretty straight forward. On to the vid…


It’s web2.0-too-many-script-ass-calling embedded video time!

Not too bad. Again, I used an Arduino Duemilanove and a Solar World panel. I might try to further this concept by incorporating my 120V switch and getting a lamp up in here. Stay tuned, as always.

Binary Red/Green Snippet

One step closer…

Switch open yields a killer red LED…

red
stop

Switch closed yields an uber-sexy green LED…

green
go

I even tossed in some 1K resistors to keep my LEDs healthy, in addition to my 100 ohm / 10000 ohm pull-down setup.

+4 resistor

I even managed to comment my code, for a bonus win:

/*
* binary red/green led setup
* by Joseph Swanson
* https://swantron.com
*/

int led1 = 11; // green LED (pin 11)
int led2 = 12; // red LED (pin 12)
int swit= 5; // switch (pin 5)
int varr; // to read pin on/off (pin 5)

void setup() {
pinMode(led1, OUTPUT); // output green
pinMode(led2, OUTPUT); // output red
pinMode(swit, INPUT); // switch input
}

void loop(){
varr = digitalRead(swit); // store swit to varr
if (varr == LOW) { // button = pressed
digitalWrite(led1, HIGH); // trigger green
digitalWrite(led2, LOW); // ground red
}
if (varr == HIGH) { // button != pressed
digitalWrite(led1, LOW); // ground green
digitalWrite(led2, HIGH); // trigger red
}
}


+1 Snippet

I’m getting closer to having this thing behave the way I intend. Stay tuned for a while longer. I’ll have a robot up and rolling in no time whatsoever.