# solar tracker using PIC16F877A

Status
Not open for further replies.

#### poxkix

##### Member level 5
I checked the forum threads having the same topic as mine but do not have the answer i'm looking for so I'll just post a new one.

Anyone got some time to help me how to make the solar using PIC16F877A?
Possible hardware used:
A) 2 LDR to detect the position of the sun
B) stepper motor / servo / DC motor
C) PIC16F877A

I have already checked the internet and none gives the detailed information on how to make them.

Basically, will be at idle. When the WEST-LDR detects the sun, it will move to where the sun is positioned and the same with the EAST-LDR. When it is night time it should return to the default position where the sun should rise again.
This is my logic and it is a little vague but I hope someone can help me make this project of mine. Any comments and suggestions would really be greatly appreciated.

My current problems:
A) MikroC code to use on the PIC16F877A
B) Solar Tracker circuit
C) Other hardware to be used.

FvM

### FvM

Points: 2
Helpful Answer Positive Rating

#### poxkix

##### Member level 5
these are already dicsussed. you should search the forum before you post new request...

https://www.edaboard.com/threads/214610/
https://www.edaboard.com/threads/155507/
Yes, as I have stated, I already checked the suggested threads when I searched. But none of the threads really helped me.

Here are a few projects which use two or more LDRs to seek a light source:

Light tracker

The design requirements are similar to the example below:

Basic Servo Motor Controlling with Microchip PIC Microcontroller - Demos light tracking with two LDRs, PIC16F690 and a servo motor

Hope the info helps,

BigDog
Thank you for the links. I found the 2nd link interesting but the code is written on a different compiler. I only know of that from mikroC. Thanks for the links though

#### ckshivaram

##### Advanced Member level 5
please put the code here and tell where is the problem, we would definetely help you.... we cant provide a code readymade for any hardware... its more of a homework kind of thing....

#### arbj

##### Full Member level 4
The project seems simple enough. There could be another alternative...

You could use a single LDR and two stepper motors, during startup the mcu could start scanning the sky for the position of the sun, the position would be indicated by the LDR, once done it could follow the motion. With this method you do not have to save position or align the LDR to the compass points.

In any case which ever project you decide on, start by identifying the components you intend to use such as the type of stepper motor, LDR etc.
Next start developing the interface hardware and basic drivers for these, you should be able to control stepper motor motion, and computer LDR values. This may also involve reading about stepper motors and LDRs

After you finished doing this start developing the algorithm for the main program (for the solar tracker device). By the time you would have done all these I am sure you will have more knowledge than before and be able to diagnose faults as well as improve the original design..

thanks
a

#### poxkix

##### Member level 5
please put the code here and tell where is the problem, we would definetely help you.... we cant provide a code readymade for any hardware... its more of a homework kind of thing....

Yes, after a few days I'll try to come up of my own code and go right back here.

The project seems simple enough. There could be another alternative...

You could use a single LDR and two stepper motors, during startup the mcu could start scanning the sky for the position of the sun, the position would be indicated by the LDR, once done it could follow the motion. With this method you do not have to save position or align the LDR to the compass points.

In any case which ever project you decide on, start by identifying the components you intend to use such as the type of stepper motor, LDR etc.
Next start developing the interface hardware and basic drivers for these, you should be able to control stepper motor motion, and computer LDR values. This may also involve reading about stepper motors and LDRs

After you finished doing this start developing the algorithm for the main program (for the solar tracker device). By the time you would have done all these I am sure you will have more knowledge than before and be able to diagnose faults as well as improve the original design..

thanks
a

Thank you for the tips. I have somewhat of the algorithm but I'm having trouble in the coding, I only know of the basics and I'm going to use mikroC. I need to look for a tutorial or something to help my pathetic self.

#### poxkix

##### Member level 5
Okay, I had done some research. I'll be using a servo motor since it meets my requirements. It was stated that the angle of a servo motor is determined by PWM. I was thinking, how will I combine the input from the photoresistor as analog and PWM?

#### bigdogguru

##### Administrator
The example I've provided above utilizes a PWM driven servo along with two LDRs. Compiling the provided code in MikroC should be straight forward. Most servos require a PWM of 50 Hz, which is far too low a frequency to generate using the PICs CCP modules effectively and requires bit banging techniques like those implemented in the example above.

I would recommend you reexamine the source code provided in the example project above and adapt it to your needs.

BigDog

FvM

### FvM

Points: 2
Helpful Answer Positive Rating

#### poxkix

##### Member level 5
The example I've provided above utilizes a PWM driven servo along with two LDRs. Compiling the provided code in MikroC should be straight forward. Most servos require a PWM of 50 Hz, which is far too low a frequency to generate using the PICs CCP modules effectively and requires bit banging techniques like those implemented in the example above.

I would recommend you reexamine the source code provided in the example project above and adapt it to your needs.

BigDog
Sir bigdogguru I tried it compiling in mikroC but it made a lot of errors and I didn't modify the code.

Okay, I'll try to study the code and make my own, this is going to be long it is going to be like a baby just starting to learn how to walk.

#### bigdogguru

##### Administrator
Okay, I'll try to study the code and make my own, this is going to be long it is going to be like a baby just starting to learn how to walk.

Yes, you must learn to walk before you run.

To add in your understanding, here is a very simple program which demonstrations the generation of three different PWMs using bit banging techniques.

Code:
//==========================================================================
//	Project				: Sample code for control single servo motors using delays
//						  This project is using 16F84A with 4MHz
//						  Has been try succesfully using GWS S03N STD
//
//						  Please make sure the hardware is compatible with this code
//
//	Project description	: This source code is used to control a servo motor.
//					  Servo motor will continuosly turn from one position to another position
//					  Lib func __delay_us() and __delay_ms() is use to generate the pulse width.
//				        The delay timing is independent of crystal but_XTAL_FREQ must be set correctly.
//
//
//	Author			: BigDogGuru
//	Date				: April 2011
//==========================================================================
//
//	include
//==========================================================================
#include "config.h"
#include "delays.h"
#include <htc.h>   					// this sample code is using 16F84A !!

//	configuration
//==========================================================================
__CONFIG ( 0x3FF2 );				// configuration for the  16F84A

//	define
//==========================================================================
#define servo	RB5					// servo set RB5

//	function 						(every function must have a function prototype)
//==========================================================================

//  none
//
//	main function					(main fucntion of the program)
//==========================================================================
void main(void)
{
unsigned int i;

//set IO port for servo pin
TRISB = 0b00000000;			//servo motor is RB1
PORTB = 0b00000000;			//clear port B

//servo will loop infinity
while(1)					//from one position to another position
{
//*******************************************************
//
//Delay determine the servo motors position
//Try change the value for different position
//
//Value in __delay_xs() function is unsigned long
//Calculate the timing correctly or servo damage could result!!
//
//******************************************************

for(i=0;i<50;i++)		//continues pulse for 50 times
{
servo=1;			//set servo pin high
delay_us(900);	//delay	0.7ms

servo=0;			//set servo pin low
delay_us(19100);	//delay 19.3ms
}

//  _	                _                  _
// | |	               | |                | |
// | |	               | |                | |              	~~50 times
// | |_________________| |________________| |____________________
// 0.7ms    19ms      0.7ms      19ms     0.7ms   19ms
// |				   |
// |<-------20ms------>|

for(i=0;i<50;i++)		//continues pulse for 50 times
{
servo=1;			//set servo pin high
delay_us(1500);	//delay	1.8ms

servo=0;			//set servo pin low
delay_us(18500);	//delay 18.2ms

}

for(i=0;i<50;i++)		//continues pulse for 50 times
{
servo=1;			//set servo pin high
delay_us(2000);	//delay	1.8ms

servo=0;			//set servo pin low
delay_us(18000);	//delay 18.2ms

}

//  ___	                ___                 ___
// |   |               |   |               |   |
// |   |               |   |               |   |              ~~50 times
// |   |_______________|   |_______________|   |____________________
//  2ms       18ms      2ms      18ms       2ms		18ms
// |				   |
// |<-------20ms------>|
}

}

//subroutines
//============================================================================
//
// none

You will need to remove the #include and _CONFIG() statements and substitute the MikroC delay functions for those in this example before compiling.

Also, it should be noted most servo can require relatively high current requires at various modes of operation, therefore you should supply the servo with its own power supply and make the grounds common between your MCU power supply.

BigDog

### FvM

Points: 2
Helpful Answer Positive Rating

### abbas.agha48

Points: 2
Helpful Answer Positive Rating

#### Sajjadkhan

##### Full Member level 5
I have an Idea about it but i haven't use it yet

1. Why are you using LDR's when solar panel can itself act as a sensor. First rotate it in 360 degree using x-axis motor, stop it at the point where you get the highest voltage. Now rotate it using y-axis motor and stop where you get the highest voltage...simple.

2. if you want to use LDR's here is some accurate idea. Use 9 LDR's in such configuration

OOO
OOO
OOO

Put them in the small black box with a hole in its center so that when Area of the solar panel is perpendicular to the sun, the ray of the sun will exactly be falling on center LDR. If sun moves the ray will be falling on the surrounding LDR'S. Now if SUN is going left, the ray will be falling on the LDR(2nd row, 3rd column) so then you know where to move your solar panel.

you can combine both methods to make it more accurate. use the 1st step for detecting where the sun is (it will bring your solar panel LDR's approx facing the sun). Then use step to accurately acquire the position (Its like fine tuning).

#### poxkix

##### Member level 5

This is the code I made
Code:
unsigned int temp_res;

void main() {
ADCON1 = 0x80;
TRISA = 0x01;
TRISB = 0x00;
//TRISD = 0x3F;

do {
temp_res = Adc_Read(0);
//temp_res=temp_res/10;
PORTB = temp_res;
}while(1);
}

I can't seem to get the right value for this. Please check

#### Sajjadkhan

##### Full Member level 5
Hey have started learning PIC too. You need to read about register ADCON0 and ADCON1 in data sheet. 2nd you are sending 10 bit result to 8bit portB. i have sent rest of the 2 bits in portC. Try reading about operators "<<" and ">>".

unsigned int temp_res;

void main() {
ADCON0 = 0x05;
ADCON1 = 0x80;
TRISA = 0x01;
TRISB = 0x00;
TRISC = 0x01;
//TRISD = 0x3F;

do {
temp_res = Adc_Read(0);
//temp_res=temp_res/10;
PORTB = temp_res;
portc = temp_res >> 8;
}while(1);
}

SK8

### SK8

Points: 2
Helpful Answer Positive Rating

#### bigdogguru

##### Administrator
I can't seem to get the right value for this.

Another issue you maybe having with your code is the use of RB6 and RB7 which are used for in-circuit debugging clock and data respectfully.

Ensure the device's configuration bits are set to disable debugging and compile your program as a "Release" build rather than "Debug" build.

BigDog

#### poxkix

##### Member level 5
Yeah but how do I make the conversion to store the digital values 1 or 0 to a variable? Let's say the if the input is like this value then the digital value should be 1. You know what I mean?

#### bigdogguru

##### Administrator
Yeah but how do I make the conversion to store the digital values 1 or 0 to a variable? Let's say the if the input is like this value then the digital value should be 1. You know what I mean?

No. Could you elaborate and provide a clearer description of your example. If you are still attempting to design a solar tracker, then use the example code above as a template and then breakdown the application into small manageable tasks.

BigDog

#### poxkix

##### Member level 5
I'm trying to sir BigDog, I'm just taking it slowly and kinda busy with other parts of my project. I hope this thread won't be forgotten. I'm going nuts!

By the way, the example works with 3 angles, is it possible to revise and make the angle vary depending on the exact/current position of the sun?

#### juhi1107

##### Newbie level 2
hey ! I am doing the same project.

Can you please share your code .. I have similar problems and am new to coding here . i am too going nuts ..I have been studying about PICs Coding for days now but in vain. I knw you will relate to me so please please share your code. Ill be grateful.THANK YOU !!

#### poxkix

##### Member level 5
hey ! I am doing the same project.

Can you please share your code .. I have similar problems and am new to coding here . i am too going nuts ..I have been studying about PICs Coding for days now but in vain. I knw you will relate to me so please please share your code. Ill be grateful.THANK YOU !!

We are on the same boat. I need 5 ldr to use

Status
Not open for further replies.