# My solar tracker code/circuit problems

#### poxkix

Joined Nov 28, 2011
42
My problem:
When the actual circuit is powered on, it works fine. I can rotate the motor clockwise/counter then after a few instances the circuit freezes and also the display at the lcd. Is there something wrong with my code and circuit?

Another problem, the motor will only stop if the ldr is in dark mode, other than that the motor will just continue until either of the ldr has the higher value then it will rotate that way.

Another, the motor moves from east to west. How can I make the motor move back to east?

For the circuit please see attached file. Purpose of the circuit is just for connection references.

Rich (BB code):
char strWest[10];
char strEast[10];
int west;
int east;

void main() {
TRISA = 0xFF;
TRISB = 0x00;
TRISC = 0x00;

Lcd_Init(&PORTB);
Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(Lcd_CURSOR_OFF);

do {
IntToStr(west,strWest);
IntToStr(east,strEast);
Lcd_Out(1,1,"WEST:");
Lcd_Out(1, 6, strWest);
Lcd_Out(2,1,"EAST:");
Lcd_Out(2,6,strEast);

if(west > east) {
PORTC.F0 = 1;
PORTC.F1 = 0;
}else if(east > west) {
PORTC.F0 = 0;
PORTC.F1 = 1;
}else if(west == east){
PORTC.F0 = 0;
PORTC.F1 = 0;
}
}while(1);
}

#### Attachments

• 76.2 KB Views: 58

#### SgtWookie

Joined Jul 17, 2007
22,221
Do you have a 10k pull-up resistor on MCLR?

Do you have the WDT and reset off?

I don't see your PICs' power pins, but you need to have a bypass capacitor (0.1uF/100nF) across them. It's a good idea to have a larger electrolytic cap in the close vicinity, too.

You may be picking up noise from the operation of the motor, particularly if the wires for the LDRs run in parallel to the motor wiring.

Showing how your board is laid out would be a help.

#### poxkix

Joined Nov 28, 2011
42
Do you have a 10k pull-up resistor on MCLR?

Do you have the WDT and reset off?

I don't see your PICs' power pins, but you need to have a bypass capacitor (0.1uF/100nF) across them. It's a good idea to have a larger electrolytic cap in the close vicinity, too.

You may be picking up noise from the operation of the motor, particularly if the wires for the LDRs run in parallel to the motor wiring.

Showing how your board is laid out would be a help.
Oh sorry, I didn't include the 10k pull-up. In my circuit I have.

What do you mean?
Do you have the WDT and reset off?

I have attached the images of my circuit. I hope it is enough. I have a bad camera

#### Attachments

• 288 KB Views: 37
• 272.1 KB Views: 21
• 263.4 KB Views: 14
• 245.4 KB Views: 15
• 240.7 KB Views: 18

#### MrChips

Joined Oct 2, 2009
23,102
No, the camera is not bad. Take photos in bright sunlight and position the camera about 2 feet away. Brace the camera on something solid or use a tripod.

#### poxkix

Joined Nov 28, 2011
42
No, the camera is not bad. Take photos in bright sunlight and position the camera about 2 feet away. Brace the camera on something solid or use a tripod.
Thanks for that. Do you know what is the problem with the posts above?

#### MrChips

Joined Oct 2, 2009
23,102
Thanks for that. Do you know what is the problem with the posts above?
Do you mean problems with the poor photography or problems with the poor electronics?

#### poxkix

Joined Nov 28, 2011
42
uhm yah you can say that. About the circuit

#### MrChips

Joined Oct 2, 2009
23,102
OK, here goes.

1. Try and keep your wires short and tidy. Do not make the wires longer than needed.

2. Follow SgtWookies instructions. Remember, he's an EXPERT.
Connect 10K at pin 1 to VCC.
I don't see any 0.1uF and 10uF capacitors on your board.

3. Put 0.1uF across the motor.

#### thatoneguy

Joined Feb 19, 2009
6,359

Instead of the string of if/else, I'd suggest using a switch/case with a default for "something odd happened" to display on LCD.

While debugging, put the raw ADC values on the other side of the screen so you can narrow down exactly what makes it stop updating.

Do you have a bypass cap between Vss and Vdd? Spurious noise with long wires will cause a processor to hang once in a while.

If you can narrow it down to "if I do this, it does that", you need to look at your code where an assumption was made related to that, and un-assume it.

To move the motor in both directions, you need to power it via H-Bridge.

#### SgtWookie

Joined Jul 17, 2007
22,221
Oh sorry, I didn't include the 10k pull-up. In my circuit I have.
You should have it on there.

What do you mean?
Do you have the WDT and reset off?
Here, have a look at this tutorial:
http://www.gooligum.com.au/tutorials/midrange/PIC_Mid_C_5.pdf
It covers midrange PIC options for Assembler and HI-TECH C that comes with MPLAB.

I forgot to mention the brown-out detection; this also could be causing problems.
I have attached the images of my circuit. I hope it is enough. I have a bad camera
Actually, the best lighting you can get is outdoors on an overcast day; the lighting is very even with no harsh shadows. Bright sun or using a flash causes very "contrasty" photos, where the details can't be picked out.

You need to be far enough away from the items you are taking photos of in order for your camera lens to be properly focused. If you have the options, set the camera to have a higher F-stop with a longer exposure time; and rest the camera on something or use a tripod when taking the photos.

#### poxkix

Joined Nov 28, 2011
42
Do you have a 10k pull-up resistor on MCLR?

Do you have the WDT and reset off?

I don't see your PICs' power pins, but you need to have a bypass capacitor (0.1uF/100nF) across them. It's a good idea to have a larger electrolytic cap in the close vicinity, too.

You may be picking up noise from the operation of the motor, particularly if the wires for the LDRs run in parallel to the motor wiring.

Showing how your board is laid out would be a help.
Yes, I have 10k pull-up on MCLR.
About WDT, I have to research about that. Especially the only language I'm familiar with is using MikroC.

Powers pins, meaning the VDD? So I'll connect the positive side of the CAP to the VDD pin and the other to the ground right?

OK, here goes.

1. Try and keep your wires short and tidy. Do not make the wires longer than needed.

2. Follow SgtWookies instructions. Remember, he's an EXPERT.
Connect 10K at pin 1 to VCC.
I don't see any 0.1uF and 10uF capacitors on your board.

3. Put 0.1uF across the motor.
Okay, I will rewire my circuit to shorten the wires. I just did it that way since it is still on prototype.
Yes, I have already 10k on pin 1

Instead of the string of if/else, I'd suggest using a switch/case with a default for "something odd happened" to display on LCD.

While debugging, put the raw ADC values on the other side of the screen so you can narrow down exactly what makes it stop updating.

Do you have a bypass cap between Vss and Vdd? Spurious noise with long wires will cause a processor to hang once in a while.

If you can narrow it down to "if I do this, it does that", you need to look at your code where an assumption was made related to that, and un-assume it.

To move the motor in both directions, you need to power it via H-Bridge.
The code I posted is the only one I have or are you looking for a specific code?
Switch/case, I can't get the right algorithm in using that code. Can you give me a sample? I find it more easier if it was if/else.
What the lcd is displaying is the raw ADC values, I converted it to string since the lcd won't display integers. Or so I might think

You should have it on there.

Here, have a look at this tutorial:
http://www.gooligum.com.au/tutorials/midrange/PIC_Mid_C_5.pdf
It covers midrange PIC options for Assembler and HI-TECH C that comes with MPLAB.

I forgot to mention the brown-out detection; this also could be causing problems.
I checked the tutorial I find it a bit baffling since I'm only familiar with MikroC
Okay, I'll search about that brown-out detection, so I should turn that off too?

#### poxkix

Joined Nov 28, 2011
42
To move the motor in both directions, you need to power it via H-Bridge.
I also made a circuit of H - bridge but I cannot get it to work. My motor power source should be 24v. The H - bridge I made only works for 12v.
Wanna see it?

#### thatoneguy

Joined Feb 19, 2009
6,359
Ok, add to the end of your if/then, after all the other tests:

else
{
LCDOut(1,1,"Something went Wrong");
}

Yes, please show diagram for H-Bridge

You may be running into the same problem with ADC Values as another member is in This Thread.

Make east and west unsigned int instead of int, not sure if it will help, but it will be more clear as to what can be stored in them if a glitch occurs.

#### poxkix

Joined Nov 28, 2011
42
Ok, add to the end of your if/then, after all the other tests:

else
{
LCDOut(1,1,"Something went Wrong");
}

Yes, please show diagram for H-Bridge

You may be running into the same problem with ADC Values as another member is in This Thread.

Make east and west unsigned int instead of int, not sure if it will help, but it will be more clear as to what can be stored in them if a glitch occurs.
Okay, I'll rewrite my code add this one. Thanks.

I think the ADC values for me has no problems. The value being read from the ldr is being displayed on the LCD though the circuit/lcd freezes. I can't test the circuit right now because I need to buy the cap needed.

Attachment for the H- Bridge I made

#### Attachments

• 89.3 KB Views: 26

#### MrChips

Joined Oct 2, 2009
23,102
You guys have given me an idea... and a challenge.

(This is not a hijack... but the mods could start a new thread if they wish.)

There is a 10,000 Year Clock now on display at the Science Museum in London, England.

http://longnow.org/

The idea of The Long Now Foundation is the brainchild of Danny Hills, inventor extraordinaire and contributor at the Whole Earth Catalog.

The principle behind the Long Now Clock is to design and build a clock that will last for a very very long time, using readily available materials, self powered and self correcting. It corrects its time by taking information from the sun.

Now, for the past year I have been wanting to build this but I was going to relax the rules. I was going to use solar powered MCU, LCD display and a sun tracker to do the resetting. I still want to do this.

I extend this challenge to members of AAC.

#### thatoneguy

Joined Feb 19, 2009
6,359
You guys have given me an idea... and a challenge.

(This is not a hijack... but the mods could start a new thread if they wish.)

There is a 10,000 Year Clock now on display at the Science Museum in London, England.

http://longnow.org/

The idea of The Long Now Foundation is the brainchild of Danny Hills, inventor extraordinaire and contributor at the Whole Earth Catalog.

The principle behind the Long Now Clock is to design and build a clock that will last for a very very long time, using readily available materials, self powered and self correcting. It corrects its time by taking information from the sun.

Now, for the past year I have been wanting to build this but I was going to relax the rules. I was going to use solar powered MCU, LCD display and a sun tracker to do the resetting. I still want to do this.

I extend this challenge to members of AAC.
Can't we all just chip in for a Really HUGE UPS for WWVB?

To OP: I'll wait to have somebody else look over your H-Bridge, does it function?

#### MrChips

Joined Oct 2, 2009
23,102
That's strange. A thread on solar tracking just disappeared. Where did it go? I though I posted the 10,000 Year Clock in that thread. Did I get moved?

#### thatoneguy

Joined Feb 19, 2009
6,359
That's strange. A thread on solar tracking just disappeared. Where did it go? I though I posted the 10,000 Year Clock in that thread. Did I get moved?

#### poxkix

Joined Nov 28, 2011
42
Can't we all just chip in for a Really HUGE UPS for WWVB?

To OP: I'll wait to have somebody else look over your H-Bridge, does it function?
It function if it is supplied for 12v, if I adjust the supply to 24v it will not work. I need a 24v supply.

By the way, the logics are used to replace the MCU.

#### poxkix

Joined Nov 28, 2011
42
Sorry for this double post but I have to, it is already 3am here and I need to finish this solar tracker

I revised the code, the revised code's purpose was suppose the make the motor turn for a certain amount of time only but for a millionth time I just can't do it. Please help the noob.

The 64 there is a value coming from the potentiometer input in the adc channel 0.

Rich (BB code):
char strWest[10];
char strEast[10];
int west;
int east;
int cnt;

void main() {
TRISA = 0xFF;
TRISB = 0x00;
TRISC = 0x00;

Lcd_Init(&PORTB);
Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(Lcd_CURSOR_OFF);

do {
IntToStr(west,strWest);
IntToStr(east,strEast);
Lcd_Out(1,1,"WEST:");
Lcd_Out(1, 6, strWest);
Lcd_Out(2,1,"EAST:");
Lcd_Out(2,6,strEast);

if(west == 64 && east < west) {
for(cnt=0;cnt<=5;cnt++) {
delay_ms(1000);
PORTC.F0 = 1;
PORTC.F1 = 0;
}
PORTC.F0 = 0;
PORTC.F1 = 0;
}else if(east > west) {
PORTC.F0 = 0;
PORTC.F1 = 1;
}else if(west == east){
PORTC.F0 = 0;
PORTC.F1 = 0;
}
}while(1);
}