Intelligent Solar Garden Light - Part 2

by zzpza in Circuits > Arduino

16463 Views, 37 Favorites, 0 Comments

Intelligent Solar Garden Light - Part 2

R0010852.JPG
Continuing from part 1

In this part I'm adding a PIR sensor.

PIR - Modification

R0010851.JPG
I wanted to make the garden light PIR activated, so have added this PIR sensor from SparkFun.

Although the sensor has an input voltage of 5V to 12V DC, it actually runs at 3.3V. So, if you bypass the voltage regulator it will run using the battery in the garden light control unit. I powered the PIR up using 5V and then using my multimeter verified where to attach the new power feed.

Attach PIR

R0010850.JPG
I've wired the positive power to the switch builtin to the light controller. The ground wire goes directly to the negative side of the battery bay. The original positive power wire for the PIR sensor is taped up so that it doesn't short anything out. The analog output from the sensor goes to analog input A1 on the Wee. On this particular sensor, it goes low (<1V) when motion is detected.

Enclosure

R0010853.JPG
Now I just need to work out what sort of enclosure to put the PIR in.

Code

(Code hacked from an RGB LED cross fade example by Clay Shirky <clay.shirky@nyu.edu>).
N.B. The led controller PCB turns the LEDs on when low, not high.

=============================================

/*
  • Intelligent Solar Powered Garden Light version 0.02 zzpza<at>truenames.co<dot>uk
  • Original code example by Clay Shirky <clay.shirky<at>nyu<dot>edu>
*/

int ledPin = 3; // LED, connected to digital pin 3
int ledVal = 0; // value to send to pin
int wait = 20; // 20ms (.02 second) delay; shorten for faster fades
int solarPin = 0; // analog pin solar panel is connected to
int solarVal; // value read from solarpanel
int PIRPin = 1; // analog pin PIR is connected to
int PIRval; // value read from PIR
int ledState = 1; // status of LED
int counter = 0; // timer for LED

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // turn leds off
ledState=0;
delay(500);
}

void fadeDownLed()
{
digitalWrite(ledPin, LOW);
Serial.println("fadeDownLed");
for (int i=0; i <= 255; i++)
{
analogWrite(ledPin, i);
// Serial.print("fadeDownLed ");
// Serial.println(i);
delay(20);
}
digitalWrite(ledPin, HIGH);
ledState=0;
counter=0;
}

void fadeUpLed()
{
digitalWrite(ledPin, HIGH);
Serial.println("fadeUpLed");
for (int i=255; i >= 0; i--)
{
analogWrite(ledPin, i);
// Serial.print("fadeUpLed ");
// Serial.println(i);
delay(20);
}
digitalWrite(ledPin, LOW);
ledState=1;
}

void turnOffLed()
{
digitalWrite(ledPin, LOW);
ledState=1;
}

void turnOnLed()
{
digitalWrite(ledPin, HIGH);
ledState=0;
}

void readSolarPanel()
{
solarVal = analogRead(solarPin);
Serial.print("Solar: ");
Serial.println(solarVal);
}

void readPIR()
{
PIRval = analogRead(PIRPin);
Serial.print("PIR: ");
Serial.println(PIRval);
}

void loop()
{
readSolarPanel();
readPIR();
if (solarVal < 100 && ledState==0 && PIRval < 5)
{
fadeUpLed();
}
if (solarVal > 120 && ledState==1 && PIRval > 5)
{
fadeDownLed();
}
if (counter > 5000) // how long LEDs should be on for
{
fadeDownLed();
}
if (ledState == 1)
{
Serial.println(counter);
counter++;
}
}