Problem in analog inputs with solar tracking and voltmeter project

hello guys. I really have a problem with a project i buid right now.i made a dual axis solar tracker using photoresistors that is working fine( i post the diagram and the code at the end).When i finished this project i tryed to improve it adding a solar panel(12V, 4.5W) and a 7segment 3digit display conected to the arduino to do a real time voltmeter and a simple 5V usb charger at the end(using a voltage regulator etc.).So i buid a voltage divider 3:1 to take the voltage from the solar panel and messuring form analog pin A5 and print it at the 3digit display.

For the solar tracking system i use adafruit motor shield, 2 stepper motors 4x10Kohm resistors and 4 photoresistors(LDR) and it is conected as the following image except tha i use steper motors directly connected to the shield and not servos Imgur: The magic of the Internet.

the code is

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *YMotor = AFMS.getStepper(4075.7728395, 2);
Adafruit_StepperMotor *XMotor = AFMS.getStepper(4075.7728395, 1);



int ph0Read ;
int ph1Read ;
int ALightPct;
int BLightPct ;


int diff1;
int diff2;
int ph2Read ;
int ph3Read ;
int CLightPct ;
int DLightPct ;

void setup() {
  Serial.begin(9600);
  AFMS.begin();

  

  YMotor->setSpeed(10);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);

  XMotor->setSpeed(10);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
}

void loop() {

  ph0Read = analogRead(A0);
  ph1Read = analogRead(A1);
  ph2Read = analogRead(A2);
  ph3Read = analogRead(A3);

  ALightPct = map(ph0Read, 0, 1023, 0, 100);
  BLightPct = map(ph1Read, 0, 1023, 0, 100);
  CLightPct = map(ph2Read, 0, 1023, 0, 100);
  DLightPct = map(ph3Read, 0, 1023, 0, 100);


  if (CLightPct < 5 && DLightPct < 5)
  {
    Serial.println("No sun detected!");
  }
  else
  {
    diff1 = CLightPct - DLightPct;
    if (abs(diff1) > 1.5)
    {
      if (diff1 > 0)
      {
        Serial.println("Turning CCW!");
        XMotor->step(25, BACKWARD, INTERLEAVE);
        delay(100);
      }
      else
      {
        Serial.println("Turning CW!");
        XMotor->step(25, FORWARD, INTERLEAVE);
        delay(100);
      }
    }

  }
  if (ALightPct < 5 && BLightPct < 5)
  {
    Serial.println("No sun detected!");
  }
  else
  {
    diff2 = ALightPct - BLightPct;
    if (abs(diff2) > 1.5)
    {
      if (diff2 > 0)
      {
        Serial.println("Turning UP!");
        YMotor->step(25, BACKWARD, INTERLEAVE);
        delay(100);
      }
      else
      {
        Serial.println("Turning DOWN!");
        YMotor->step(25, FORWARD, INTERLEAVE);
        delay(100);
      }
    }

  }
}

For the voltmeter project i use a voltage divider 300 Ohm/ 100 Ohm and the circuit is this Imgur: The magic of the Internet
and the code

int a;
int b;
int c;
int vin;
int input=A5; 
int disp1=2;
int disp2=3;
int disp3=4;
int segA=5;
int segB=6;
int segC=7;
int segD=8;
int segE=9;
int segF=10;
int segG=11;
int segDP=12;
const float vpp = 0.0048875855327468;
int  voltage;
float digivolt;

void setup()
{
  Serial.begin(9600);
    
pinMode(disp1, OUTPUT);
pinMode(disp2, OUTPUT);
pinMode(disp3, OUTPUT);
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
pinMode(segDP, OUTPUT);
pinMode(input,INPUT);
}


int display (int a)
{
switch (a)
{
case 0:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
break;

case 1:
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

case 2:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
break;

case 3:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
break;

case 4:
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 5:
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 6:
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 7:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

case 8:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 9:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;
}}
void loop()
{
vin=analogRead(input);
digivolt =(vin * vpp)*4;
voltage = (vin * vpp)*4*10;
a = voltage%10;

digitalWrite(disp1,LOW);
digitalWrite(disp2,LOW);
digitalWrite(disp3, HIGH);
digitalWrite(segDP,HIGH);
display(a);
delay(5);
b = voltage/10;
a = b%10;
digitalWrite(disp1,LOW);
digitalWrite(disp2,HIGH);
digitalWrite(disp3, LOW);
digitalWrite(segDP,LOW);
display(a);
delay(5);
c=b/10;
a=c;
digitalWrite(disp1,HIGH);
digitalWrite(disp2,LOW);
digitalWrite(disp3, LOW);
digitalWrite(segDP,HIGH);
display(a);
delay(5);
Serial.println(digivolt);
Serial.println(voltage);

}

the completed circuit is this Imgur: The magic of the Internet

SO HERE IS THE PROBLEM :
I can't run the tracking program if the A5 or GRD of the voltmeter is conected to arduino.
If i load the voltmeter programm it is working fine.But the solar tracking programm works only if i disconect the A5 input from the ''voltmeter'' circuit.If A5 is connected the program doesnt start at all.if i disconect A5 it starts.and if i start the programm with A5 unconnected and while it is runnig i connect A5 it still runs until i give some light at one of the photoresistors and then it freeze.

It seems very complicated i know but if anyone has a solution for me or find what i did wrong please share it with me.That will be very helpfull cause it is my graduation project and it really means a lot.
Thank you

What voltage are you giving your analog port? Remember it's a maximum of 5 volts, so if you hope to measure more you will need a voltage divider (and then multiply up in your code).

Increase the resistors of the voltage divider by at least factor 100 (better 1000) and try again.

Ralph_S_Bacon:
What voltage are you giving your analog port? Remember it's a maximum of 5 volts, so if you hope to measure more you will need a voltage divider (and then multiply up in your code).

i am using a voltage divider check my circuit and my code you will see it

We don't like going off site for images, read how to do it correctly image guide here.

I can't run the tracking program if the A5 or GRD of the voltmeter is conected to arduino.

That does not seem to make any sense, that "schematic" does not show a voltmeter.

Are you using I2C interface anywhere? As that will use A4 & A5.

I am assuming when you say GRD you mean ground GND.

You don't show any power lines nor how the stepping motors are connected to the Arduino.

pylon:
Increase the resistors of the voltage divider by at least factor 100 (better 1000) and try again.

Hello. Idk why but after expiramenting with your advise i fount out that no matter the R1 value (of course bigger or equal to R2 ) , using an R2 equal or biger of 15 kOhm (i used R2=22kohms and R1=88kOhms) the project runs fine.
Of course the printed values were wrong and they started from 13V to 20V but with a simple map function [map(voltage, 130 ,200, 0, 200);] it really worked fine.
Thank you very much..!

Hi,
Ops circuit


Tom... :slight_smile:

Hi,
Which motor shield, link to spec/data.

Tom... :slight_smile:

The shield uses the SDA and SCL i2c pins to control DC and stepper motors. On the Arduino UNO these are also known as A4 and A5.

I think that's the problem, if V2 driver shield.
Tom... :slight_smile:

TomGeorge:
I think that's the problem, if V2 driver shield.
Tom... :slight_smile:

Yeah its V2 adafruit motor shield...but now that i changet the voltage diviter resistors values it is working fine.

kechos:
Yeah its V2 adafruit motor shield...but now that i changet the voltage diviter resistors values it is working fine.

No it can not be working fine using the same pins for two purposes. You haven't tested it sufficiently.

Grumpy_Mike:
No it can not be working fine using the same pins for two purposes. You haven't tested it sufficiently.

Hello.Actually the manual of the shield says:

"The shield uses the SDA and SCL i2c pins to control DC and stepper motors. On the Arduino UNO these are also known as A4 and A5. On the Mega these are also known as Digital 20 and 21. On the Leonardo these are also known as digital 2 and 3. Do not use those pins on those Arduinos with this shield with anything other than an i2c sensor/driver.

Since the shield uses I2C to communicate, you can connect any other i2c sensor or driver to the SDA/SCL pins as long as they do not use address 0x60 (the default address of the shield) or 0x70 (the 'all call' address that this chip uses for group-control)"

So i can use them.The problem here is that the voltmeter technique i use isnt i2c and it still runs but problematicaly cause when i light the photoresistors (pins A0,A1,A2,A3) that gives the order to move the motors the LED display is flickering.I don't know if this is happenig because of my programm (proper delays etc.) or i have to build an I2C connection.

Hi,
What values did you change them to?

LED display is flickering.I don't know if this is happenig because of my programm (proper delays etc.)

Yes it is because of your code, you are using software to multiplex the display, this is very time sensitive, the more you get your program to do, the more you will have flicker problems.

You need a display like this, and it will save you I/O pins too.

Tom... :slight_smile:

Hi,
Where abouts in your circuit diagram is your power source, how are you powering the steppers.

Can you spend a bit more time on your diagram and avoid crossing wires diagonally, if they have to cross each other, please do it at right angles.

Tom.. :slight_smile:

TomGeorge:
Hi,
What values did you change them to?Yes it is because of your code, you are using software to multiplex the display, this is very time sensitive, the more you get your program to do, the more you will have flicker problems.

Hi,at first i used R1=300Ohms R2=100ohms and now i use r1=66Kohms and 22kohms.
Here is the thing,when LDR are not taking light the display is ok,the blinking starts when LDR take light so the stepper motors spin..the voltage mesurements from the panel are fine but they flick on the display my code is

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_StepperMotor *YMotor = AFMS.getStepper(4075.7728395, 2);
Adafruit_StepperMotor *XMotor = AFMS.getStepper(4075.7728395, 1);

int ph0Read = 0;
int ph1Read = 0;
int ALightPct = 0;
int BLightPct = 0;


int diff1;
int diff2;
int ph2Read = 0;
int ph3Read = 0;
int CLightPct = 0;
int DLightPct = 0;


int a;
int b;
int c;
int vin;
int input=A5; 
int disp1=2;
int disp2=3;
int disp3=4;
int segA=5;
int segB=6;
int segC=7;
int segD=8;
int segE=9;
int segF=10;
int segG=11;
int segDP=12;
const float vpp = 0.0048875855327468;
int  voltage;
float digivolt;
int realvolt;


int display (int a)
{
switch (a)
{
case 0:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
break;

case 1:
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

case 2:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
break;

case 3:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
break;

case 4:
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 5:
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 6:
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 7:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

case 8:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 9:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;
}}

void setup() {
    Serial.begin(9600);
    AFMS.begin();
    
    YMotor->setSpeed(10);
    pinMode(A0,INPUT);
    pinMode(A1,INPUT);
    
    XMotor->setSpeed(10);
    pinMode(A2,INPUT);
    pinMode(A3,INPUT);


    pinMode(disp1, OUTPUT);
    pinMode(disp2, OUTPUT);
    pinMode(disp3, OUTPUT);
    pinMode(segA, OUTPUT);
    pinMode(segB, OUTPUT);
    pinMode(segC, OUTPUT);
    pinMode(segD, OUTPUT);
    pinMode(segE, OUTPUT);
    pinMode(segF, OUTPUT);
    pinMode(segG, OUTPUT);
    pinMode(segDP, OUTPUT);
    pinMode(input,INPUT);
}

void loop() {
  
   
    vin=analogRead(input);
    digivolt =(vin * vpp)*4;
    voltage = (vin * vpp)*4*10;
    realvolt=map(voltage, 130, 200, 0, 200);
    
    
    a = realvolt%10;
    digitalWrite(disp1,LOW);
    digitalWrite(disp2,LOW);
    digitalWrite(disp3, HIGH);
    digitalWrite(segDP,HIGH);
    display(a);
    delay(8);
    
    b = realvolt/10;
    a = b%10;
    digitalWrite(disp1,LOW);
    digitalWrite(disp2,HIGH);
    digitalWrite(disp3, LOW);
    digitalWrite(segDP,LOW);
    display(a);
    delay(8);
    
    c=b/10;
    a=c;
    digitalWrite(disp1,HIGH);
    digitalWrite(disp2,LOW);
    digitalWrite(disp3, LOW);
    digitalWrite(segDP,HIGH);
    display(a);
    delay(8);
    
    
    
    
    ph0Read=analogRead(A0);
    ph1Read=analogRead(A1);
    ph2Read=analogRead(A2);
    ph3Read=analogRead(A3);
   
    
    ALightPct = map(ph0Read, 0, 1023, 0, 100);
    BLightPct = map(ph1Read, 0, 1023, 0, 100);
    CLightPct = map(ph2Read, 0, 1023, 0, 100);
    DLightPct = map(ph3Read, 0, 1023, 0, 100);
    
   
    if(CLightPct < 5 && DLightPct < 5)
    {
        Serial.println("No sun detected!");
    }
    else
    {
      diff1= CLightPct - DLightPct;
      if(abs(diff1)>1)
      {
          if(diff1>0)
          {
          Serial.println("Turning CCW!");
          XMotor->step(25, BACKWARD,INTERLEAVE);
          delay(25); 
          }
          else
          {
             Serial.println("Turning CW!");
            XMotor->step(25,FORWARD,INTERLEAVE);
            delay(25); 
          }
      }
      
    }   
    if(ALightPct < 5 && BLightPct < 5)
    {
        Serial.println("No sun detected!");
    }
    else
    {
       diff2= ALightPct - BLightPct;
       if(abs(diff2)>1)
        {
            if (diff2>0)
            {
              Serial.println("Turning UP!");
              YMotor->step(25, BACKWARD,INTERLEAVE);
              delay(25);
            }
            else
            {
              Serial.println("Turning DOWN!");
              YMotor->step(25,FORWARD,INTERLEAVE);
              delay(25); 
            }
        }
     
      }
    }

Can i solve the blinking issue without changing the display(by programing i guess)?
Imade a better diagram i thing it helps.
Steppers are powered by 12V from the shield by external source and also ABCDEFG and Dot pins are conected with 220 Ohm resistors and digits1,2,3 are directly conected to arduino sorry for not including it at diagram

So i can use them.

How you come to that conclusion after reading what you did is beyond me. You have a Uno, you are using a shield that uses I2C so you can not use A5 as an analogue input. That is what those words said.

Grumpy_Mike:
How you come to that conclusion after reading what you did is beyond me. You have a Uno, you are using a shield that uses I2C so you can not use A5 as an analogue input. That is what those words said.

Hi, when i started the project i didn't know that pins A4 and A5 are only for i2c as adarfruit suggests so i used them and as far it works.Do you suggest to make an i2c connection (with an ATtiny maybe) for the voltage analog read?
Could you check the flickering problem and suggest a possible solution?

Hi,
I have redrawn your schematic and there are some problems with it.
The a to g and dp wires need current limit resistors for the LED segments.
The K1 to K3 wires need a NPN transistor in each one to switch the current needed by the display cathodes, the UNO output pins cannot supply the required current as it is wired.
How are you powering the UNO and the steppers?


Tom... :slight_smile:

Hi,
In post #12, I suggested a solution to your flicker, it is a dedicated 7seg display that does not have to be continually updated.

All the UNO boards I have do not label A4 and A5 as SDA and SCL, yet point out which pins are PWM.

Why yours new configuration works, probably to do with the voltage level and the load on the I2C line imposed by your potential divider.
But as stated before, its better to leave the A4 and A5 alone to I2C.

Tom.. :slight_smile:
Feel free to edit my schematic, use Paint or GIMP or what ever loads jpg.

TomGeorge:
Hi,
I have redrawn your schematic and there are some problems with it.
The a to g and dp wires need current limit resistors for the LED segments.
The K1 to K3 wires need a NPN transistor in each one to switch the current needed by the display cathodes, the UNO output pins cannot supply the required current as it is wired.

Hi again, nice schematic..I use 220 Ohm resistors at a to g and dp wires.I tried the NPN transistor a few times at K1 to K3 and it didn't worked at all cause none of the leds lighted up so i removed the transistors.

I understand that it is not proper to use i2c A5 and A4 for analog iput so maybe i will take the messurment from an ATtiny85 but the messurments i take with this confuguration are pretty accurate...
So i cant do anything with this circuit about the flickering? Is it nessesery to change the LED segments?
I power up UNO and steppers (from the shield) with external 12V battery