Wind Sensor, LED's and Arduino

Interfacing hardware, sensors, output drivers, chips.
Post Reply
emersonb
Posts: 9
Joined: Wed May 12, 2010 2:03 pm

Wind Sensor, LED's and Arduino

Post by emersonb » Mon May 17, 2010 2:33 pm

I would like to blow on the wind sensor and have it light up LED's.
I have a row of 10 LED's.
A slowly building wind would light them in sequence from left to right until all 10 would be lit.

Here is an image of my setup with Wind Sensor, LED's and Arduino:
[img=400x400]http://mydata.salve.edu/emersonb/post/led_wind.jpg[/img]

Here is the code I was trying to get to work with no luck.

Code: Select all

// Controlling LED's using a wind sensor (variable resistor?) 

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //pins used for LED's
int windSen = 2;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 

void setup() 
{
 // set all pins to output
 for (int x = 0; x < 10; x++) {
   pinMode (ledPin[x], OUTPUT); }
}

void loop()
{
 val = analogRead(windSen);		// reads the value of the wind sensor (value between ? and ?)
  for (int i = 0; i < 100; i++) {
    if( val < 5) { digitalWrite(ledPin[0], HIGH);}
else
     { digitalWrite(ledPin[0], LOW);}
    if( (i * 10) > val) { digitalWrite(ledPin[i], HIGH);}
    else
     { digitalWrite(ledPin[i], LOW);}
  }
}  
Any help to fix setup and code wold be great!
Thanks,
emersonb-
Attachments
led_wind.jpg
wind sensor setup
(24.37 KiB) Downloaded 8954 times

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: Wind Sensor, LED's and Arduino

Post by paul » Mon May 17, 2010 4:21 pm

emersonb ,

Get the wind sensor working, and set the pot with the Examples -> Stubs -> AnalogReadSerial sketch, so you know you have good data coming in.
I advise setting the no wind zero to around 50 (AD units).

Then just threshold the output like this

Code: Select all

if (value > 100){ digitalWrite(LED1, HIGH);  }
if (value > 120){ digitalWrite(LED2, HIGH);  }
//etc.


That will create a bargraph of the wind value - if you want a "moving dot" instead - just use two conditions to turn on the LED's

if (value > 100 && value < 120){ digitalWrite(LED1, HIGH); }

I personally think it will be more impressive if you use analogWrite() to dim the LED's though

Paul

emersonb
Posts: 9
Joined: Wed May 12, 2010 2:03 pm

Re: Wind Sensor, LED's and Arduino

Post by emersonb » Mon May 17, 2010 9:24 pm

Paul -
I can't seem to get to 50 (AD units) no matter the direction of the potentiometer.
Do I need to modify the Arduino (Examples/Stubs/AnalogReadSerial) sketch to get the right data?

This is the code I'm using to test for data:

Code: Select all

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(2);
  Serial.println(sensorValue, DEC);
}
Attached is a picture of the random numbers I get and the setting at half way.
Thanks,
Attachments
wind_sensor_pot.jpg
(35.18 KiB) Downloaded 8949 times

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: Wind Sensor, LED's and Arduino

Post by paul » Mon May 17, 2010 10:20 pm

Do you have it plugged into the analog block - GND, +5 , A2 (referring to your sketch)
My guess is
1) the sensor isn't powered OR
2) you're reading a different channel on the ADC than the one the sensor is plugged into

Paul

emersonb
Posts: 9
Joined: Wed May 12, 2010 2:03 pm

Re: Wind Sensor, LED's and Arduino

Post by emersonb » Tue May 18, 2010 4:53 am

Paul
I replaced the wires and now I am getting good readings.

How would you write the "analogWrite() to dim the LED's"?
This method would turn all LED's an and off slowly based on wind speed?

I tried this with not much luck, some lights start on dim and others do not light at all.

Code: Select all

// Controlling LED's using a wind sensor

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //pins used for LED's

void setup() 
{
  Serial.begin(9600);
 // set all pins to output
 for (int x = 0; x < 10; x++) {
   pinMode (ledPin[x], OUTPUT); }
}

void loop()
{
  int sensorValue = analogRead(2);   //wind sensor range from a distance 100 - 500
  Serial.println(sensorValue, DEC);
  
 for (int i = 0; i < 10; i++) {
    analogWrite(ledPin[i], sensorValue-245); // controls brightness 0-255
  }
}   
I will post my other experiments.
That seemed to go as planned thanks for all the tips.
emersonb-

emersonb
Posts: 9
Joined: Wed May 12, 2010 2:03 pm

Re: Wind Sensor, LED's and Arduino

Post by emersonb » Tue May 18, 2010 2:11 pm

Paul-
It did not work before due to the placement of the capacitor.
Where does the optional capacitor go in between?
emersonb-

emersonb
Posts: 9
Joined: Wed May 12, 2010 2:03 pm

Re: Wind Sensor, LED's, Servo and Arduino

Post by emersonb » Tue May 18, 2010 4:26 pm

Any Ideas for running a servo with LED's and wind sensor?
In my code I think the delay of the servo slows down the reaction time of the LED's.
Any other ways to do this?

Code: Select all

// Wind sensor and Servo Sweep

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //pins used for LED's
#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(3);  // attaches the servo on pin 3 to the servo object
  //Serial.begin(9600);
 // set all pins to output
 for (int x = 0; x < 10; x++) {
   pinMode (ledPin[x], OUTPUT); }
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
  int sensorValue = analogRead(2);
  //Serial.println(sensorValue, DEC);
  
    if (sensorValue > 160){ digitalWrite(ledPin[0], HIGH);  }
    if (sensorValue > 180){ digitalWrite(ledPin[1], HIGH);  }
    if (sensorValue > 200){ digitalWrite(ledPin[2], HIGH);  }
    if (sensorValue > 220){ digitalWrite(ledPin[3], HIGH);  }
    if (sensorValue > 240){ digitalWrite(ledPin[4], HIGH);  }
    if (sensorValue > 260){ digitalWrite(ledPin[5], HIGH);  }
    if (sensorValue > 280){ digitalWrite(ledPin[6], HIGH);  }
    if (sensorValue > 300){ digitalWrite(ledPin[7], HIGH);  }
    if (sensorValue > 320){ digitalWrite(ledPin[8], HIGH);  }
    if (sensorValue > 340){ digitalWrite(ledPin[9], HIGH);  }
     
    if (sensorValue < 160){ digitalWrite(ledPin[0], LOW);  }
    if (sensorValue < 180){ digitalWrite(ledPin[1], LOW);  }
    if (sensorValue < 200){ digitalWrite(ledPin[2], LOW);  }
    if (sensorValue < 220){ digitalWrite(ledPin[3], LOW);  }
    if (sensorValue < 240){ digitalWrite(ledPin[4], LOW);  }
    if (sensorValue < 260){ digitalWrite(ledPin[5], LOW);  }
    if (sensorValue < 280){ digitalWrite(ledPin[6], LOW);  }
    if (sensorValue < 300){ digitalWrite(ledPin[7], LOW);  }
    if (sensorValue < 320){ digitalWrite(ledPin[8], LOW);  }
    if (sensorValue < 340){ digitalWrite(ledPin[9], LOW);  }
} 

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: Wind Sensor, LED's and Arduino

Post by paul » Tue May 18, 2010 7:32 pm

Emerson,

The wind sensor definitely doesn't need the capacitor, it's entirely optional. It just cuts out some of the high frequency (>10 hz) jitter you probably won't want anyway.

I'd treat each of the issues of wind sensor, LED's, Servo separately. Get them all going independently - then combine them, and = you'll know if you are getting system effects from interactions, (mostly from the servo). You may need a separate power supply for the servo to isolate all it's high current draw and power line noise from the rest of the system.

Paul

emersonb
Posts: 9
Joined: Wed May 12, 2010 2:03 pm

Re: Wind Sensor, LED's and Arduino

Post by emersonb » Tue May 18, 2010 10:42 pm

Here are my final results with the Wind Sensor. The windsock is made from christmas tree tinsel.
It is positioned behind the fork of the Wind Sensor. The windsocks movement shows actual wind and the corresponding LED result.
LED's light up more based on wind strength.

Here is a link to what it looks like in action:
http://mydata.salve.edu/emersonb/post/led_wind_bar.html

Code: Select all

// Controlling LED's using a wind sensor
// Wind speed turns on LED's in a row, more wind more LED's

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //pins used for LED's

void setup() 
{
  Serial.begin(9600);
 // set all pins to output
 for (int x = 0; x < 10; x++) {
   pinMode (ledPin[x], OUTPUT); }
}

void loop()
{
  int sensorValue = analogRead(2);
  Serial.println(sensorValue, DEC);
  
    if (sensorValue > 160){ digitalWrite(ledPin[0], HIGH);  }
    if (sensorValue > 180){ digitalWrite(ledPin[1], HIGH);  }
    if (sensorValue > 200){ digitalWrite(ledPin[2], HIGH);  }
    if (sensorValue > 220){ digitalWrite(ledPin[3], HIGH);  }
    if (sensorValue > 240){ digitalWrite(ledPin[4], HIGH);  }
    if (sensorValue > 260){ digitalWrite(ledPin[5], HIGH);  }
    if (sensorValue > 280){ digitalWrite(ledPin[6], HIGH);  }
    if (sensorValue > 300){ digitalWrite(ledPin[7], HIGH);  }
    if (sensorValue > 320){ digitalWrite(ledPin[8], HIGH);  }
    if (sensorValue > 340){ digitalWrite(ledPin[9], HIGH);  }
     
    if (sensorValue < 160){ digitalWrite(ledPin[0], LOW);  }
    if (sensorValue < 180){ digitalWrite(ledPin[1], LOW);  }
    if (sensorValue < 200){ digitalWrite(ledPin[2], LOW);  }
    if (sensorValue < 220){ digitalWrite(ledPin[3], LOW);  }
    if (sensorValue < 240){ digitalWrite(ledPin[4], LOW);  }
    if (sensorValue < 260){ digitalWrite(ledPin[5], LOW);  }
    if (sensorValue < 280){ digitalWrite(ledPin[6], LOW);  }
    if (sensorValue < 300){ digitalWrite(ledPin[7], LOW);  }
    if (sensorValue < 320){ digitalWrite(ledPin[8], LOW);  }
    if (sensorValue < 340){ digitalWrite(ledPin[9], LOW);  }
}  
If this code could be made better let me know.
Thanks
Attachments
led_wind_bar.jpg
(19.05 KiB) Downloaded 8939 times

emersonb
Posts: 9
Joined: Wed May 12, 2010 2:03 pm

Re: Wind Sensor, LED's and Arduino

Post by emersonb » Tue May 18, 2010 10:53 pm

This Wind Sensor experiment is similar to the last one. The windsocks movement shows actual wind and the corresponding LED result.
Only one LED lights up at a time. More wind strength lights up the next LED in line.

Here is a link to what it looks like in action:
http://mydata.salve.edu/emersonb/post/led_wind_dot.html

Code: Select all

// Controlling LED's using a wind sensor
// Wind speed turns on one LED in a row, more wind lights the next LED.

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //pins used for LED's

void setup() 
{
  Serial.begin(9600);
 // set all pins to output
 for (int x = 0; x < 10; x++) {
   pinMode (ledPin[x], OUTPUT); }
}

void loop()
{
  int sensorValue = analogRead(2);
  Serial.println(sensorValue, DEC);
  
 //delay(500);
  for (int i = 0; i < 10; i++)
  digitalWrite(ledPin[i], LOW);
    if (sensorValue > 160 && sensorValue < 190){ digitalWrite(ledPin[0], HIGH); }
    if (sensorValue > 190 && sensorValue < 220){ digitalWrite(ledPin[1], HIGH); }
    if (sensorValue > 220 && sensorValue < 250){ digitalWrite(ledPin[2], HIGH); }
    if (sensorValue > 250 && sensorValue < 280){ digitalWrite(ledPin[3], HIGH); }
    if (sensorValue > 280 && sensorValue < 310){ digitalWrite(ledPin[4], HIGH); }
    if (sensorValue > 310 && sensorValue < 340){ digitalWrite(ledPin[5], HIGH); }
    if (sensorValue > 340 && sensorValue < 370){ digitalWrite(ledPin[6], HIGH); }
    if (sensorValue > 370 && sensorValue < 400){ digitalWrite(ledPin[7], HIGH); }
    if (sensorValue > 400 && sensorValue < 430){ digitalWrite(ledPin[8], HIGH); }
    if (sensorValue > 430 && sensorValue < 450){ digitalWrite(ledPin[9], HIGH); }
}  
If this code could be made better let me know.
Thanks
Attachments
led_wind_dot.jpg
(15.79 KiB) Downloaded 8939 times

emersonb
Posts: 9
Joined: Wed May 12, 2010 2:03 pm

Re: Wind Sensor, LED's and Arduino

Post by emersonb » Tue May 18, 2010 11:04 pm

Paul-
FYI: I have the capacitor working between: "out" on the Wind Sensor and the "analog input" (in my case: 2) on the Arduino.

I'm still interested in how you would light all LED's on with the sensor based on wind strength.
more wind = dim to bright and less wind dim to dark . . .

Can you give me some code to get me started?
emersonb-

Post Reply