How to Create a Solar Panel that Tracks the Sun


    Although this guide shows how to create a control system to make a solar panel track the sun, its main purpose is to show how to write your own program for a PC to read analogue inputs and then control stepper motors according to your requirements. As such, the principles learned here pave the way to a wide range of other applications. The focus of this guide is mostly on the hardware and software to implement the control system with only a passing reference to the mechanics involved in linking the stepper motors to the solar panel.
   We will be using two boards from the “Bee Range" for this control system. They are the “WASP”, which will provide the analogue inputs and the “StepperBee”, which provides the drive for both stepper motors. To implement the software we will be using the multi-board DLL (Beehive) provided as a free download at the following link.

        Connecting both boards to the PC is trivial and just involves connecting them to a free USB port using a standard lead. There are no drivers to be installed as they are already part of Windows (WinXP or later). Windows will just recognise the devices and auto-configure your system to use them.  With the two control boards ready for use we now need to look at the external devices and how to connect them to the boards.

The Sensor Inputs
Since we want to be able to track the sun we need some form of light detector. There are a number of alternatives here, but the simplest is an LDR (Light Dependant Resistor).

LDR light sensor (light dependant resistor)circuit for using a light dependant resistor for measuring light intensity    The principle on which these work is that their resistance decreases as the light intensity increases. Typically it is about 1M ohm in darkness, 10K ohm in moderate light and 2K ohm or less in bright sunlight). Because of the relatively high impedance of the analogue inputs on the WASP we can connect the LDR’s directly using the circuit shown on the right, where the resistor used is 10K ohm.
    This will cause the voltage appearing at the WASP input to vary between about 1v and 4v in average daylight conditions, which is more than enough to base our control system on. Note that we have 4 LDR’s connected to the WASP. This will allow us to mount two in the vertical plane and two in the horizontal. They will also be angled with respect to each other. As the sun rises into the sky the upper one of the vertical pair will receive more effective light since the sun is closer to being perpendicular to its surface while the lower will receive less. Similarly as the sun traverses east to west the two in the horizontal plane will receive different amounts of light. This is illustrated below

diagram of how to make a solar panel which automatically tracks the sun

   The function of our control system is to move the solar panel in such a way as to keep the received light on each LDR in a pair about the same.

The Stepper Motor Outputs
standard 4 pahse unipolar stepper motor for use with pc compatible computers    For our purposes we have chosen two 12v Unipolar, 4-phase, 1.8degree per step, bi-directional stepper motors with a medium torque output. These represent one of the most common types in everyday use and provide an economical solution for our control system. Ignoring, for the moment, the mechanical arrangement of the stepper motors, their connection to the StepperBee is just a two-minute job with a small screwdriver. The circuit and photo of this arrangement is shown below.

how to connect two stepper motors to a PC

photo showing how to connect stepper motors to a standard PC compatible computer

    The mechanical arrangement of the stepper motors and the solar panel must be such that one stepper motor can control the vertical angle of the panel whilst the other can rotate a platform on which it is sits (i.e. the east-west orientation). This is shown diagrammatically below.

overview of a solar panel sun tracking system using stepper motors and light sensors

    The precise details of how to arrange the mechanical linkages to achieve the movement of the solar panel are deliberately avoided here, since as mentioned earlier, we are focussing on the control system. However, there are a couple of points to consider: Depending on how large the solar panel is, you may need some gearing on the stepper motor output shaft to provide the necessary torque and degree of control required. You should also consider the weather proofing aspect unless the whole arrangement is behind glass or similar.

The Software
    Before starting the task of writing our own software we should test all inputs and outputs manually using the BeeHive application. Beehive is actually two distinct parts. One is a DLL for programmers to use and the other is an application which uses the DLL to provide immediate access to manual control of any connected board. Installation of the software is simply a matter of following the instructions on the install screens. When you run Beehive you will have a multi-tabbed environment divided into a separate screen for each type of board supported and one screen for a master control. The master control page is the first page you see.  Simply press the “Scan for Devices” button on this page to see a list of the boards you have connected. You should see the total boards set at ‘2’ and the list showing a “WASP” and a “StepperBee”. This tells you everything is okay as far as the USB and PC interface is concerned.

    You can then select the screen for the WASP. This screen has a box showing how many WASP’s are connected. It should say ‘1’.   Since Beehive is designed for multiples of each board you must now select which board of the available WASPs you want to control. In our case simply select number ‘1’.
Pressing the scan button will now cause the analogue inputs on the attached WASP to be continuously scanned and their level updated on the 4 vertical slider controls. Waving your hand over the LDR sensors should confirm they are responding ok to the varying light conditions.

To confirm correct operation of the Stepper motors we now go to the StepperBee screen.  The screen box showing how many Stepper Bees are connected should say ‘1’. As with the WASP you should Select number ‘1’ for the choice of which board to control. The manual controls for the StepperBee are simple to use. To test motor ‘1’ set the steps to 200, the interval to 20ms and the direction to forward. Click on the “run” button and you should see motor 1 do one complete revolution in about 4 seconds. Repeat this for motor 2.

    Assuming you have no problems with the above, you have verified all hardware and USB communications are working correctly so we can now progress to writing our own software.

    For the purposes of explanation we will be using Visual Basic as the programming language. Although the beehive DLL can be used with almost any language that supports function calls to a DLL (including C, C++ etc…) , visual basic is generally understood to an extent by most programmers. (For specific details about using the DLL with C++, see the beehive documentation).

    The first step in using the DLL is to copy it from the directory where you downloaded it to a directory where your compiler will find it. The safest option here is c:\windows\system32, which is included as part of the directory search of most programs.

Preparing your program to use the DLL requires a few formalities….
      At the head of your program you need to declare all of the functions you will be using within the DLL…

Declare Function InitBee Lib "bee.dll" () As Integer

Declare Function GetBoardType Lib "bee.dll" (ByVal devnum As Integer) As Integer

Declare Function WSP_SetOutputs Lib "bee.dll" (ByVal devnum As Integer, ByVal outputs As Integer) As Integer

Declare Function WSP_ReadInputs Lib "bee.dll" (ByVal devnum As Integer, ByRef analogue As Integer) As Integer

Declare Function WSP_SetSensitivity Lib "bee.dll" (ByVal devnum As Integer, ByVal sensitivity As Integer) As Integer

Declare Function STP_RunMotor1 Lib "bee.dll" (ByVal devnum As Integer, ByVal steps As Integer, ByVal interval As Integer, ByVal direction As Integer, ByVal outputs As Integer) As Integer

Declare Function STP_RunMotor2 Lib "bee.dll" (ByVal devnum As Integer, ByVal steps As Integer, ByVal interval As Integer, ByVal direction As Integer, ByVal outputs As Integer) As Integer

Declare Function STP_StopMotor1 Lib "bee.dll" (ByVal devnum As Integer, ByVal outputs As Integer) As Integer

Declare Function STP_StopMotor2 Lib "bee.dll" (ByVal devnum As Integer, ByVal outputs As Integer) As Integer

Declare Function STP_SetStepMode Lib "bee.dll" (ByVal devnum As Integer, ByVal M1Mode As Integer, ByVal M2Mode As Integer) As Integer

Declare Function STP_GetStatus Lib "bee.dll" (ByVal devnum As Integer, ByRef M1Active As Integer, ByRef M2Active As Integer, ByRef M1Steps As Integer, ByRef M2Steps As Integer, ByRef Inputs As Integer) As Integer

    The first step within your program must be the initialisation of the “bee” environment, which prepares the USB communications and searches for all attached devices. This is initiated by calling the following function ….

Dim NumBoards As Integer

NumBoards = InitBee()

   After this call, NumBoards holds the total number of “Bee” boards attached to the system, which in this case should be 2. You then need to find out what the device numbers are for your attached boards. A device number is the unique identifier of a particular board in the system. This is done by checking the type of each board as follows….

Dim Type As Integer
Dim DevNum As Integer
Dim WaspDevNum As Integer
Dim StepperDevNum As Integer
For Devnum=1 To NumBoards Step 1

Type = GetBoardType(DevNum)

If Type=BRD_WASP Then
WaspDevNum = DevNum
End If

StepperDevNum = DevNum
End If

Next DevNum

    The constants BRD_WASP and BRD_STEPPER are defined in the file be.h which is supplied with the DLL and should be included at the head of your program.
The result of running the section of program above is to have the unique device number for the WASP in the variable WaspDevNum and the one for the StepperBee in StepperDevNum. These number should be preserved and used whenever any of the subsequent function calls to these boards are needed.

    If we are going to move the stepper motors according to the analogue readings obtained from the LDR sensors then there we need to look at the following functions…

To read the four analogue inputs we can use the WSP_ReadInputs() function call as follows…

Dim Analogue(4) As Integer
WSP_ReadInputs( WaspDevNum, Analogue(0) )

This will result in the values of the 4 analogue inputs being in the array elements Analogue(0) … Analogue(3)

To move a Stepper motor in a particular direction by a given amount at a given speed we need to use…

Dim Steps As Integer
Dim Interval As Integer
Dim Direction As Integer
Dim Outputs As Integer
Steps = 200
STP_RunMotor1(StepperDevNum, Steps, Interval, Direction, Outputs)

    This example will cause motor 1 to do 200 steps (i.e. one complete revolution) clockwise in about 4 seconds (i.e. 200 x 20ms). The outputs variable is not used here but setting any of the first three bits of this variable will cause the corresponding switching output on the stepper bee to be turned on. Similarly we can operate motor 2 using the function STP_RunMotor2(….)
     With these basic functions available to the program it can now be organised to carry out the control system as required. For the control system to do its job and move the solar panel so that it follows the path of the sun, we need to construct some basic logic to implement this. At this point we are digressing from the control techniques to the more mundane general programming practices which differ and vary according to individual programmers and their own style. Without going into the precise details of this code we can outline some basic principles.
     We, essentially, have two independent control system working together. One for the horizontal (east west) axis and one for the vertical. Each control system has two sensors and one stepper motor. If we consider the vertical axis first and assume we are using LDR1 at the top of the panel and LDR2 at the bottom. We will also assume the forward direction of the motor raises the panel and the reverse direction lowers it. This leads to the following logic to perform our control system….

If the analogue voltage from LDR1 is greater than that from LDR2 then move stepper motor 1 forward by 1 step.

If the analogue voltage from LDR1 is less than that from LDR2 then move stepper motor 1 in reverse by 1 step.

    This will result in the panel moving in the vertical plane until the light is equally balanced between the two sensors, which is exactly what we need. The horizontal movement logic is exactly the same substituting LDR3 and 4 for LDR1 and 2 and motor2 for motor1.
     Although the above equations offer a simple neat set of logic for our control which is very easy to implement in VBasic, we need to consider some more practical considerations. We need to consider how fast to react to imbalances in the two LDR readings. It may be, for example, that as clouds pass across the sun the shadow will cause the move quickly in one direction then back in the other. It would be better to build in some latency in this process to “smooth out” any sudden variations.    After all, we don’t want the solar panel tracking the shadow of a plane as it passes over !
    We also need to consider the steady state situation when the light on the LDR’s are approximately equal. If we use the above equations strictly we will be moving the panel for even a single digit difference in the analogue voltage level. This would probably lead to continuous movement about the balance point. This phenomenon is called “hunting”. To get round this we need to introduce a “dead zone” where the panel is stationary until the difference is great enough to warrant movement.
    Although we will not elaborate on the code to program these functions here it should be obvious that it is relatively trivial and achievable in a number of ways.


!!! You now have a solar panel which automatically tracks the sun  !!!




.        .


© Copyright 2008