5 years, 1 month ago.

Single Axis Sun Tracker

Hello everyone, I have been working on this project and I managed to assemble and wire everything and started programming and ran in to couple of problems. First one being that when I try to control the stepper motor by using the inputs from two LDRs it doesn't work as I want to. The two LDRs' voltage difference (threshold) to rotate the motor CW or CCW is having some difficulties. When LDR1 is giving more voltage than LDR2 the motor moves CCW and vice versa. But the problem is that when the LDRs' voltage difference is less than that of the threshold it keeps rotating by one step CW and then CCW and doesn't stop, I tried introducing another logic that stops the motor when the voltage difference of the LDRs is lower than the threshold, but that somehow caused the motor to not rotate at all even when the difference was higher than the threshold. I kept the threshold at 0.1 (which is 10% of 5V being used) and been trying to fix this. Any help will be greatly appreciated.

Second problem is the LCD display, I tried displaying a few words on the LCD, as can be seen from the code below, by using an external trigger but the LCD is only displaying blocks throughout the 16x2 screen. I wired the LCD as it says in the cookbook.

Thanks, here is the code.

#include "mbed.h"
#include "sMotor.h"
#include "TextLCD.h"

AnalogIn Vr1(p15);                           // Input from LDR1 - West
AnalogIn Vr2(p16);                           // Input from LDR2 - East
sMotor motor(p26, p27, p28, p29);            // Outputs to L293D - IN1, IN2, IN3, IN4
DigitalOut myled1(LED1);                     // Indicator for anti-clockwise rotation
DigitalOut myled2(LED4);                     // Indicator for clockwise rotation
DigitalOut myled3(LED2);                     // Indicator for stopped panel
DigitalOut myled4(LED3);                     // Indicator for panel reset
AnalogIn Vz(p17);                            // Input from zener diode - For LCD
TextLCD lcd(p19, p20, p21, p22, p23, p24, TextLCD::LCD16x2);   // Outputs to LCD - rs, e, d4, d5, d6, d7 
Timer t;                                     // Timer for resetting the panel

int step_speed = 1200 ;                      // Sets default motor speed
int numstep = 512 ;                           // Total steps for 360 degree rotation

int main() {               
    while(1) {
            if (Vr1-Vr2>0.1) {                                       // Logic for stepping the motor anti-clockwise
                    motor.step(numstep/36, 0, step_speed);
                    wait (5);
                    myled1=0;    }
            if (Vr2-Vr1>0.1) {
                    motor.step(numstep/36, 1, step_speed);                // Logic for stepping the motor clockwise
                    wait (5);
                    myled2=0;    }
          if (Vz==0)  {                                                // Logic for battery status display on LCD
              lcd.printf("Charging");        }
         else         {
                lcd.printf("Charged");       }  
Comment on this question

3 Answers

5 years, 1 month ago.

Regarding the LCD, triple check your wiring. That is the default setup situation when there has been no communication with the LCD from the mbed.

For your motor, what happens when you don't give any commands to the motor?

Well it doesn't move. But with LDRs commected and as the only inputs to move the motor, and it doesnt stop even when the voltage difference between them is lower than 10%

posted by Mohamed Sulaiman 21 Mar 2014
5 years, 1 month ago.

Your actuator is probably moving to fast compared to the sun's position and speed that makes it overshoot and then move back and fourth. I would set the code to slow the movements to more gradual steps when the two voltages get close to each other. When both voltages are much the same but not exact, put a delay timer in to sample every 5 minutes then read the values again to make small movements. There will always be a slight offset error doing this. That will stop the hunting problem.

The other method and probably be better for reasons below would be get to the exact position where the voltages are the same then run code to step the motors slowly to track the sun at the same speed. You will need to experiment with the stepping rate to cover the whole day and perhaps run the voltage check once per hour. This way if the sun is obscured by cloud the panels will still be on track and will not hunt around. You may need to put a voltage threshold to hold off any correction should the sun be obscured when running the re-aligning code.

It would be great if you could help with the small movements. Plus I am not testing it with sunlight, I am doing so with my flash light, because during demonstration I would be using that setup.

posted by Mohamed Sulaiman 22 Mar 2014
5 years, 1 month ago.

It would be great if you could help with the small movements. Plus I am not testing it with sunlight, I am doing so with my flash light, because during demonstration I would be using that setup.

To post an answer, please log in.