Sunflower - mako

Go to content
Arduino > Projects
Arduino Sunflower
Arduino Sunflower is an Arduino UNO solar tracker consisting of 2 LDRs and a servomotor.
Using a light against the two LDRs the flower will turn left or right. It is important to position the LDR in the same way as the servo rotation, in order to have a realistic experience.

Things used in this project
  • Arduino UNO
  • 1 servo motor
  • 2 photoresistors
  • 10 jumpers


int sensorPin = A0; 
int servoPin  = 9; 
int sensorValue = 0; 
int servoGrad = 90; 
int tolerance = 40;  
Servo myservo; 
void setup() {   
pinMode( sensorPin, INPUT);   
myservo.attach( servoPin );   
myservo.write( servoGrad ); 

void loop() {   
sensorValue = analogRead(sensorPin);   
if ( sensorValue < (512-tolerance) )   
  if (servoGrad < 180) servoGrad++;
if ( sensorValue > (512+tolerance) )   
if (servoGrad > 0) servoGrad--;   
myservo.write( servoGrad );     


Sketch analysis
Servo.h: include the Servo.h class, which simplifies all servomotor management.

int sensorPin = A0: set the integer variable to which you assign the pin to which you connect the sensor, in this example A0.

int servoPin = 9: set the integer variable to which you assign the pin to which you connect the servomotor, in this example 9.

int sensorValue = 0: set an integer variable to which you will assign the value read by the sensor in loop phase ().

int servoGrad = 90: sets an integer variable that indicates the position, in degrees, of the servo, in the sketch start phase I position the servo value at 90 °, if you want to start from a different angle you can vary this value.

int tollerance = 40: set an integer variable to which you assign a tolerance within which the servo must not rotate even if the sensor detects variations from the central value, you will see better later what it is for.

Servo myservo: initializes the Servo object by assigning it to the myservo instance.

pinMode( sensorPin, INPUT): set the pin to which the sensor is connected in INPUT mode.

myservo.attach( servoPin ): with the attach (pin) method of the myservo object you indicate which pin the servomotor is connected to.

myservo.write( servoGrad ): with the write (grad) method you set the degrees to which the servo must position itself, during the setup phase () the value will be the one defined on line 16.

sensorValue = analogRead(sensorPin): read the value coming from the analog pin (A0) with the analogRead () function and assign it to the sensorValue variable defined on line 15.

if( sensorValue < (512-tollerance) ): carry out the first check if the value read from line 28 is less than the difference between 512 and the tolerance set you are in the condition in which the panel is no longer oriented in front of the sun, one of the two photo resistors is detecting an intensity greater than the other, which one depends on how you connected the resistors, in my example it is the right resistor photo and to adjust the position of the panel I have to rotate beyond the initial 90 degrees, between 90 ° and 180 °.

if(servoGrad < 180) servoGrad++: perform a further check, that the value in degrees assumed by the servo is not> 180 as the servo you are using would not be able to go further anyway, if the servo angle is <180 ° then you can increase the degrees of rotation. The expression servoGrad ++ indicates to increase the value of servoGrad by one degree from 90 to 91.92.93 etc ... at each arduino loop ().

if( sensorValue > (512+tollerance) ): verifies a condition similar to that of line 29. In this case the value read by the sensor is compared with the sum between 512 + tolerance.

if(servoGrad > 0) servoGrad--: check that the angle the servo is at is not less than 0, even in this case it would not be possible for the servomotor to perform the required movement. It then decreases the value of servoGrad one degree for each loop () in which the described conditions occur;
myservo.write( servoGrad ): it's time to make the servo move, reuse the write method (degrees) to get the servo to move to the desired position.

delay(100): wait a time of 100 milliseconds between one detection and the next, this allows the servo to reach the indicated position before the detection of the sensors changes. You can increase this value by many thousands as the sun takes hours to move.
There are no reviews yet.
Back to content