Creating a Sunflower Solar Panel using Arduino
Have you ever wanted to create your own sunflower solar panel that follows the movement of the sun? With a light sensor, a servo motor, and an Arduino board, it's easier than you might think! In this guide, we'll walk you through each line of code and explain how it all works in simple terms .
circuit diagram of servo motor at light intensity greater than 100 lumens
circuit diagram of servo motor at light intensity less than 100 lumens
DOWNLOAD PROTEUS FILE AND CODE```
#include <Servo.h>
```
First, we include the Servo library which allows us to control servo motors with our Arduino board.
The Servo library helps an Arduino board control servo motor. Servo motors have gears and a shaft that can be moved to different positions. The Servo library can control many servos at once and has several methods for controlling them. These methods let you attach a servo motor to an Arduino board, move it to a specific position or speed, read its current position, check if it’s attached, and detach it.
```
int lightpin = A5;
int lightval;
int servpin=9;
int servpos = 180;
int servpos2 = 0;
Servo myservo;
int i;
int delaytime = 50;
```
We then define several variables. `lightpin` is set to `A5`, which means we will be using analog pin 5 to read the value of our light sensor. `lightval` will store the value read from the light sensor. `servpin` is set to `9`, which means we will be using digital pin 9 to control our servo motor. `servpos` and `servpos2` are set to `180` and `0` respectively, which represent the two positions our servo motor will move between. We also create a `Servo` object called `myservo`, which we will use to control our servo motor. Variable `i` will be used later in a for loop. `delaytime` is set to `50`, which represents the amount of time (in milliseconds) we will wait between each step of our servo motor.
```
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(servpin);
pinMode(lightpin,INPUT);
}
```
In the `setup()` function, we initialize serial communication at a baud rate of 9600. Baud rate refers to the speed at which data is transmitted over a communication channel. In simpler terms, it's like the speed limit on a road - it determines how fast information can travel between two devices. In this case, data is transmitted at a rate of 9600 bits per second.
We then attach our servo motor to `servpin`, and set `lightpin` as an input.
```
void loop() {
myservo.write(servpos2);
// put your main code here, to run repeatedly:
lightval = analogRead(lightpin);
Serial.println(lightval);
while ( true) {
if(lightval<=201){
myservo.write(servpos);
lightval = analogRead(lightpin);
Serial.println(lightval);
delay(delaytime);}
else if (lightval>201){
for(i=0;i<180;i=i+30){
myservo.write(i);
delay(delaytime);
lightval = analogRead(lightpin);
delay(delaytime);
Serial.println(lightval);
}
}
}
}
```
In the `loop()` function, we first move our servo motor to position `servpos2`. We then read the value of our light sensor using the `analogRead()` function and store it in `lightval`. This value is also printed to the serial monitor using the `Serial.println()` function.
Next, we enter an infinite loop using a `while(true)` statement. Inside this loop, we check if the value of `lightval` is less than or equal to 201.
If it is, we move our servo motor to position `servpos`, read the value of our light sensor again using the `analogRead()` function and store it in `lightval`, print it to the serial monitor using the `Serial.println()` function, and wait for a short period of time using the `delay()` function with argument `delaytime`.
NOTE:The value of 201 was obtained through experimentation by connecting an LDR (light-dependent resistor) to an Arduino board and reading its value in both darkness and sunlight using the serial monitor (the circuit diagram is the same as the one used for the project, its down below). In a Proteus simulation, the value for 0.1 lumens on the analog scale is 1023, while 1000 lumens is 34. Lumens is the unit of measurement for light intensity. It’s important to note that these values were obtained through simulation and may differ in real-life scenarios where a machine would be needed to accurately measure light intensity.According to our research, a solar panel starts charging at 100 lumens. Using the values obtained from testing the LDR in Proteus, we found that 100 lumens corresponds to a value of 201 on the analog scale.
If the value of `lightval` is greater than 201, we enter a for loop that increments variable `i` from 0 to 180 in steps of 30. Inside this loop, we move our servo motor to position `i` using the `write()` function with argument `i`, wait for a short period of time using the `delay()` function with argument `
Arduibo boards at 6200 naira ,3 day delivery: Buy your arduino board NIGERIA
Cheap arduino boards :Buy your arduino board WORLDWIDE.
No comments: