twitter
Search

Entries in sensors (5)

Sunday
Feb192012

Sparkfun RGB LED and RGB Light Sensor

I can't quite figure this one out: it's VERY delicate and I can't get consistant color results with the built-on LED on or off. I know if it gets a UV light, it shows a lot of purple and if it gets hit with a red laser it maxes out the red LED. Anyhow... Video and code! 

 

 

/*
Expanded to use the Sparkfun RGB LED on pins 3, 5, and 6, sending
PWM signals as 1/10 of the result. YouTube video of this in action at
Use of the RGB LED changes added by thoughtfix 2/2012
Inherited OSHW license.
Original comments below.
*/
/*
An Arduino code example for interfacing with the 
HDJD-S822-QR999 Color Sensor.  Put an object in front of the
sensor and look at the serial monitor to see the values the sensor
is reading.  Scaling factors and gains may have to be adjusted
for your application.
by: Jordan McConnell
 SparkFun Electronics
 created on: 1/24/12
 license: OSHW 1.0, http://freedomdefined.org/OSHW
 
Connect the gain pins of the sensor to digital pins 7 - 12 (or ground).
Connect the led pin to digital 13.
Connect Vr to analog 0, Vg to analog 1, and Vb to analog 2.
*/
// Define pins
const int ledpin = 13;
const int GSR1 = 12;
const int GSR0 = 11;
const int GSG1 = 10;
const int GSG0 = 9;
const int GSB1 = 8;
const int GSB0 = 7;
const int redLed = 3;
const int greenLed = 5;
const int blueLed = 6;
int redpin = A0;
int greenpin = A1;
int bluepin = A2;
// Sensor read values
int red = 0;
int green = 0;
int blue = 0;
int redBright = 0;
int greenBright = 0;
int blueBright = 0;
void setup() 
{
  Serial.begin(9600);
  pinMode(ledpin, OUTPUT);
  pinMode(GSR1, OUTPUT);
  pinMode(GSR0, OUTPUT);
  pinMode(GSG1, OUTPUT);
  pinMode(GSG0, OUTPUT);
  pinMode(GSB1, OUTPUT);
  pinMode(GSB0, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  // Turn on the LED
  digitalWrite(ledpin, LOW);
  
  // Set the gain of each sensor
  digitalWrite(GSR1, LOW);
  digitalWrite(GSR0, LOW);
  digitalWrite(GSG1, LOW);
  digitalWrite(GSG0, LOW);
  digitalWrite(GSB1, LOW);
  digitalWrite(GSB0, LOW);
}
void loop() 
{
  
  // Read sensors
  // On page 7 of the datasheet, there is a graph of the 
  // spectral responsivity of the chip.  Scaling factors were
  // selected based on this graph so that the gain of each 
  // color is closer to being equal
  red = analogRead(redpin) * 10;
  green = analogRead(greenpin) * 14;
  blue = analogRead(bluepin) * 17;
  redBright = red/10;
  greenBright = green/10;
  blueBright = blue/10;
  // Print values to the serial monitor
  Serial.print("Red: ");
  Serial.print(red, DEC);
  analogWrite(redLed, redBright);
  Serial.print("\t\tGreen: ");
  Serial.print(green, DEC);
  analogWrite(greenLed, greenBright);
  Serial.print("\tBlue: ");
  Serial.println(blue, DEC);
  analogWrite(blueLed, blueBright);
  delay(20);
}
/*Expanded to use the Sparkfun RGB LED on pins 3, 5, and 6, sendingPWM signals as 1/10 of the result. YouTube video of this in action at
Use of the RGB LED changes added by thoughtfix 2/2012Inherited OSHW license.Original comments below.*/
/*An Arduino code example for interfacing with the HDJD-S822-QR999 Color Sensor.  Put an object in front of thesensor and look at the serial monitor to see the values the sensoris reading.  Scaling factors and gains may have to be adjustedfor your application.
by: Jordan McConnell SparkFun Electronics created on: 1/24/12 license: OSHW 1.0, http://freedomdefined.org/OSHW Connect the gain pins of the sensor to digital pins 7 - 12 (or ground).Connect the led pin to digital 13.Connect Vr to analog 0, Vg to analog 1, and Vb to analog 2.*/

// Define pinsconst int ledpin = 13;const int GSR1 = 12;const int GSR0 = 11;const int GSG1 = 10;const int GSG0 = 9;const int GSB1 = 8;const int GSB0 = 7;const int redLed = 3;const int greenLed = 5;const int blueLed = 6;
int redpin = A0;int greenpin = A1;int bluepin = A2;
// Sensor read valuesint red = 0;int green = 0;int blue = 0;int redBright = 0;int greenBright = 0;int blueBright = 0;
void setup() {  Serial.begin(9600);
  pinMode(ledpin, OUTPUT);  pinMode(GSR1, OUTPUT);  pinMode(GSR0, OUTPUT);  pinMode(GSG1, OUTPUT);  pinMode(GSG0, OUTPUT);  pinMode(GSB1, OUTPUT);  pinMode(GSB0, OUTPUT);  pinMode(redLed, OUTPUT);  pinMode(greenLed, OUTPUT);  pinMode(blueLed, OUTPUT);
  // Turn on the LED  digitalWrite(ledpin, LOW);    // Set the gain of each sensor  digitalWrite(GSR1, LOW);  digitalWrite(GSR0, LOW);  digitalWrite(GSG1, LOW);  digitalWrite(GSG0, LOW);  digitalWrite(GSB1, LOW);  digitalWrite(GSB0, LOW);}
void loop() {    // Read sensors  // On page 7 of the datasheet, there is a graph of the   // spectral responsivity of the chip.  Scaling factors were  // selected based on this graph so that the gain of each   // color is closer to being equal  red = analogRead(redpin) * 10;  green = analogRead(greenpin) * 14;  blue = analogRead(bluepin) * 17;  redBright = red/10;  greenBright = green/10;  blueBright = blue/10;
  // Print values to the serial monitor  Serial.print("Red: ");  Serial.print(red, DEC);  analogWrite(redLed, redBright);  Serial.print("\t\tGreen: ");  Serial.print(green, DEC);  analogWrite(greenLed, greenBright);  Serial.print("\tBlue: ");  Serial.println(blue, DEC);  analogWrite(blueLed, blueBright);  delay(20);}

 

Wednesday
Feb012012

Twitter-posting Kitty Dishes

For Adafruit/Instructables "Make it Tweet" challenge, I assembled a way for an Arduino microcontroller to monitor and post updates to Twitter when the cats eat, need food, and need water. Here's an overview video:

The complete project is posted on instructables, complete with source code and schematics.

Wednesday
Feb012012

Arduino Haunted Pumpkin

For the Radio Shack Great Create, I crafted a motion-sensitive haunted pumpkin with pulsing red eyes. When motion was detected, it would blink wildly and issue a wicked laugh. The full how-to with pictures and video is posted on instructables.

Full source code:

 

/*
This is the source code for the motion sensitive
haunted pumpkin built for Radio Shack.
Original code by Daniel Gentleman, thoughtfix.com
*/

// Set up pin assignments
int leftEye = 3; // PWM pin 3
int rightEye = 5; // PWM pin 5
int redBlink1 = 9;
int redBlink2 = 10;
int whiteBlink = 13;
int laughBox = 12; // transistor to voice module
int pirSensor = 7; // passive infrared sensors
int pirState = 0; //Initial IR state

// Setting up pin modes
void setup() {
pinMode(leftEye, OUTPUT);
pinMode(rightEye, OUTPUT);
pinMode(redBlink1, OUTPUT);
pinMode(redBlink2, OUTPUT);
pinMode(whiteBlink, OUTPUT);
pinMode(laughBox, OUTPUT);
pinMode(pirSensor, INPUT);
}

void loop () {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(leftEye, fadeValue);
analogWrite(rightEye, fadeValue);
// wait for 30 milliseconds
delay(30);
}
pirState = digitalRead(pirSensor); // read the state of the pirsensor value:
if (pirState == HIGH){ // If motion is detected
freakout(); // Call the freakout routine
}

// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(leftEye, fadeValue);
analogWrite(rightEye, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
pirState = digitalRead(pirSensor); // Same as above
if (pirState == HIGH){
freakout();
}

}

void freakout(){
digitalWrite(laughBox, HIGH); // Send intial ON for all pins
digitalWrite(leftEye, HIGH);
digitalWrite(rightEye, HIGH);
digitalWrite(redBlink1, HIGH);
digitalWrite(redBlink2, HIGH);
digitalWrite(whiteBlink, HIGH);
delay(250);
digitalWrite(laughBox, LOW); // turn off laugh button
for (int i=1; i<=40; i++){ // Start blinking mayhem
digitalWrite(leftEye, HIGH);
digitalWrite(rightEye, HIGH);
digitalWrite(redBlink1, LOW);
digitalWrite(redBlink2, LOW);
digitalWrite(whiteBlink, LOW);
delay(50);
digitalWrite(leftEye, LOW);
digitalWrite(rightEye, LOW);
digitalWrite(redBlink1, HIGH);
digitalWrite(redBlink2, HIGH);
digitalWrite(whiteBlink, HIGH);
delay(50);
} // Finish blinking mayhem, turn everything off
digitalWrite(redBlink1, LOW);
digitalWrite(redBlink2, LOW);
digitalWrite(whiteBlink, LOW);
}

Wednesday
Feb012012

Ethernet enabled server room thermometer

After my first "blinky" thermometer was taken down for looking "too unprofessional," I moved on to craft a more subtle (and functional) way to get the job done. This time,  with an Arduino Uno, an Ethernet Shield, and a DHT11 temperature sensor. The code is a combination of Ladyada's DHT code and the Arduino example Ethernet code with only tiny modifications. The enclosure was purchased at Jameco.

The result is monitored in Nagios and Cacti. If anyone builds one and needs help with Nagios/Cacti implementation, let me know.

The HTML output is intentionally boring so as to be easily readable by monitoring software:

TemperatureC: 28.00
TemperatureF: 82.40
Humidity: 53.00

Code:

/*
  Web  Server Thermometer

A simple web server that shows the value of a DHT11
using an Arduino Wiznet Ethernet shield. 

Circuit:
* DHT11 attached to pin 2
* Ethernet shield attached to pins 10, 11, 12, 13

Web server code: 
created 18 Dec 2009
by David A. Mellis
modified 4 Sep 2010
by Tom Igoe

DHT code written by ladyada
Public Domain

Combining the two by thoughtfix
July, 2011

*/

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"

#define DHTTYPE DHT11   // DHT 11 
#define DHTPIN 2     // DHT pin

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x4B, 0x21 };
byte ip[] = { 192,168,1, 223 };

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
Server server(80);
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  // start the Ethernet connection and the server:

  Ethernet.begin(mac, ip);
  server.begin();
  dht.begin();
}


void loop()
{
  // listen for incoming clients
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = t*1.8+32; 
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          // output the value of each analog input pin
          client.print("TemperatureC: "); 
          client.println(t);
          client.println("<br />");
          client.print("TemperatureF: "); 
          client.println(f);
          client.println("<br />");
          client.print("Humidity: "); 
          client.print(h);
          client.println("<br />");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Wednesday
Feb012012

Arduino server room thermometer (Blinky)

This is the first generation of the project with only visual alerts. The second generation was Ethernet enabled.

At my last job, I rarely had to set foot in my server room. The air conditioning unit, however, sometimes falls behind in the summer. I wanted something that would grab the attention of people walking by if it were to go over 80F ambient temperature. Except for a little wiring, the parts list is 100% Adafruit stock:

 


That's $31.75 if you have an UNO handy, $30 more if you don't. 

To build the LCD shield: http://forums.adafruit.com/viewtopic.php?f=31&p=112887#p112887

A long section of ribbon cable (I used 7 wires - more than I need) to go around the door and a little wire, time, and solder to assemble the bits together are all that is left. I have the proto shield's LEDs on pins 12 and 13, the LCD shield exactly how I described it built in a previous thread, and the temperature sensor on (analog 0) pin 14. I have analog 1 and 2 going to the back breadboard too just in case I want to use them.

On the ribbon cable, I am using VIN to power the display. This way the power supply can sit inside the server room and won't be hanging off the wall in the "public area." The barrel jack was perfect for that. 

Make sure to get the required libraries for the temperature sensor and LCD from the product links above or it'll never compile. The code is ugly and poorly commented, but it does the job. I even got lazy and read the temperature through two different functions (one as a string, one as an int) because I hate converting back and forth. 

Code:

// Arduino "FREAK OUT OVER 80F" thermometer

#include <PCD8544.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "String.h"
#include "WProgram.h"



// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 14

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// arrays to hold device address
DeviceAddress insideThermometer;
PCD8544 nokia = PCD8544(7, 6, 5, 4, 3);
char msg[125];
int flash = 0;
int currTemp;

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");
  // locate devices on the bus
  Serial.print("Locating devices...");
  sensors.begin();
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");
  // report parasite power requirements
  Serial.print("Parasite power is: "); 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(insideThermometer);
  Serial.println();
  // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  sensors.setResolution(insideThermometer, 9);
  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(insideThermometer), DEC); 
  Serial.println();
  nokia.init();
  nokia.command(PCD8544_DISPLAYCONTROL | PCD8544_DISPLAYALLON);
  delay(500);
  // back to normal
  nokia.command(PCD8544_DISPLAYCONTROL | PCD8544_DISPLAYNORMAL);
  pinMode(13, OUTPUT); // Green  LED
  pinMode(12, OUTPUT); // Red LED 
  digitalWrite(12, HIGH);
}


void loop(void)

  nokia.clear();
  sensors.requestTemperatures(); // Send the command to get temperatures
  String msgHead = "The current   server room   temp is ";
  String termMessage = msgHead + fetchTemp(insideThermometer) + "F " + "\0";
  Serial.println(termMessage);
  int msglen = (termMessage.length()+1); // count the characters, add 3 just in case
  Serial.print(msglen);
  Serial.println(" - message length");
  termMessage.toCharArray(msg,msglen); // Convert it to an array called msg
  Serial.print("Attempted message ");
  Serial.println(msg); 
  nokia.drawstring(0, 0, msg);
  nokia.display();
  if (numTemp(insideThermometer) > 80){
    for (int flash = 1; flash < 10; flash++){
    digitalWrite(13, HIGH);
    digitalWrite(12, LOW);
    delay(60);
    digitalWrite(12, HIGH);
    digitalWrite(13, LOW);
    delay(60);
    }
  }
  else {
  delay(600);
  }
  nokia.clear();

}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

String fetchTemp(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  int tempF = DallasTemperature::toFahrenheit(tempC);
  String reportTemp = tempF;
  String reportText = reportTemp + "F";
  return reportTemp;
}

int numTemp(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  int tempF = DallasTemperature::toFahrenheit(tempC);
  return tempF;
}