Error translating analogue voltage & LEDs

Hi

I’ve just purchased a Leonardo and plan to use it to drive programmable LEDs and I’m having a weird error.

I have a company called electric pedals that uses bicycles to power outdoor cinema. Because we use ultra capacitors and not batteries, it’s critical that during a performance that we are aware of the capacitor voltage. If people don’t pedal enough and the voltage falls to a low level, everything turns off. Traditionally we use small LED displays, but of course they are difficult to see from a distance.

So I plan to build a LED tower:

4x 1m strips of 60 addressable LEDs (pololu)
1x Arduino Leonardo
2x DCDC 12v to 5v Power supplies (15W)

I’m using pins:

a0: Vin (from external system)
gnd: Vo
12: LED controller

I’ve already written the code and tested (see below) it and it works perfectly.

This is the issue:

When I disconnect the computer and connect Arduino to an external power supply, it appears that the readings I’m getting from analogRead are nonsense.

Could it be some sort of GND issue?

Thanks
Colin

#include <PololuLedStrip.h>
int incomingByte = 0; 

// Create an ledStrip object and specify the pin it will use.
PololuLedStrip<12> ledStrip;

// Create a buffer for holding the colors (3 bytes per color).
#define LED_COUNT 60
rgb_color colors[LED_COUNT];
int offset; 
int delayvalue;

const float referenceVolts = 16;        // the default reference on a 5-volt board
//const float referenceVolts = 3.3;  // use this for a 3.3-volt board

const float R1 = 15000; // value for a maximum voltage of 10 volts
const float R2 = 6560;
// determine by voltage divider resistors, see text
const float resistorFactor = 1023.0 / (R2/(R1 + R2));  
const int batteryPin = 0;         // +V from battery is connected to analog pin 0

void setup()
{
  
  Serial.begin(9600);
  
  //turn LEDs off
   for (int i = 0; i < LED_COUNT; i++)
    {
      colors[i] = (rgb_color){0, 0, 0};
    }
  
  ledStrip.write(colors, LED_COUNT); 
  
}


void loop()
{
      
      delayvalue = 1200;
      int val = analogRead(batteryPin);  // read the value from the sensor
      float volts = (val / resistorFactor) * referenceVolts ; // calculate the rati
      volts = ((referenceVolts/5)*volts);  // print the value in volts
      Serial.println(volts);
   
      //turn off all LEDs
      for (int j = 0; j < LED_COUNT; j++)
      {
        colors[j] = (rgb_color){0, 0, 0};
      }
      
      //bottom 10 red
      for (int j = 0; j < 10; j++)
      {
        colors[j] = (rgb_color){255, 0, 0};
      }
      
       //top 10 green
      for (int j = 50; j < LED_COUNT; j++)
      {
        colors[j] = (rgb_color){0, 255, 0};
      }
      
      //position based on voltage
      if (volts < 13)
      {
         offset=11;
         delayvalue = 100;
      }
      else if (volts >= 13 && volts < 13.05)
      {
         offset = 11;
         delayvalue = 200;
      }
      else if (volts >= 13.05 && volts < 13.10)
      {
         offset = 12;
         delayvalue = 300;
      }
      else if (volts >= 13.10 && volts < 13.15)
      {
         offset = 13;
         delayvalue = 400;
      }
      else if (volts >= 13.15 && volts < 13.20)
      {
         offset = 13;
         delayvalue = 500;
      }
      else if (volts >= 13.20 && volts < 13.25)
      {
         offset = 13;
         delayvalue = 600;
      }
      else if (volts >= 13.25 && volts < 13.30)
      {
         offset = 14;
         delayvalue = 700;
      }
      else if (volts >= 13.30 && volts < 13.35)
      {
         offset = 15;
         delayvalue = 800;
      }
      else if (volts >= 13.35 && volts < 13.40)
      {
         offset = 16;
         delayvalue = 900;
      }
      else if (volts >= 13.40 && volts < 13.45)
      {
         offset = 17;
         delayvalue = 1000;
      }
      else if (volts >= 13.45 && volts < 13.50)
      {
         offset = 17;
         delayvalue = 1100;
      }
      else if (volts >= 13.50 && volts < 13.55)
      {
         offset = 18;
         delayvalue = 1200;
      }
      else if (volts >= 13.55 && volts < 13.60)
      {
         offset = 19;
      }
      else if (volts >= 13.60 && volts < 13.65)
      {
         offset = 20;
      }
      else if (volts >= 13.65 && volts < 13.70)
      {
         offset = 20;
      }
      else if (volts >= 13.70 && volts < 13.75)
      {
         offset = 21;
      }
      else if (volts >= 13.75 && volts < 13.80)
      {
         offset = 22;
      }
      else if (volts >= 13.80 && volts < 13.85)
      {
         offset = 23;
      }
      else if (volts >= 13.85 && volts < 13.90)
      {
         offset = 24;
      }
      else if (volts >= 13.90 && volts < 13.95)
      {
         offset = 24;
      }
      else if (volts >= 13.95 && volts < 14.00)
      {
         offset = 25;
      }
      else if (volts >= 14.00 && volts < 14.05)
      {
         offset = 26;
      }
      else if (volts >= 14.05 && volts < 14.10)
      {
         offset = 27;
      }
      else if (volts >= 14.10 && volts < 14.15)
      {
         offset = 28;
      }
      else if (volts >= 14.15 && volts < 14.20)
      {
         offset = 28;
      }
      else if (volts >= 14.20 && volts < 14.25)
      {
         offset = 29;
      }
      else if (volts >= 14.25 && volts < 14.30)
      {
         offset = 30;
      }
      else if (volts >= 14.30 && volts < 14.35)
      {
         offset = 31;
      }
      else if (volts >= 14.35 && volts < 14.40)
      {
         offset = 32;
      }
      else if (volts >= 14.40 && volts < 14.45)
      {
         offset = 32;
      }
      else if (volts >= 14.45 && volts < 14.50)
      {
         offset = 33;
      }
      else if (volts >= 14.50 && volts < 14.55)
      {
         offset = 34;
      }
      else if (volts >= 14.55 && volts < 14.60)
      {
         offset = 35;
      }
      else if (volts >= 14.60 && volts < 14.65)
      {
         offset = 35;
      }
      else if (volts >= 14.65 && volts < 14.70)
      {
         offset = 36;
      }
      else if (volts >= 14.70 && volts < 14.75)
      {
         offset = 37;
      }
      else if (volts >= 14.75 && volts < 14.8)
      {
         offset = 38;
      }
      else if (volts >= 14.80 && volts < 14.85)
      {
         offset = 39;
      }
      else if (volts >= 14.85 && volts < 14.90)
      {
         offset = 40;
      }
      else if (volts >= 14.90)
      {
         offset = 41;
      }
      else
      {}
 
      for (int j = offset-1; j < offset+9; j++)
      {
        colors[j] = (rgb_color){255, 255, 255};
      }
      
      ledStrip.write(colors, LED_COUNT); 
      
      //delay(delayvalue);
      
      if (volts <= 13.5)
      {  
        for (int j = 0; j < 10; j++)
        {
          //colors[j] = (rgb_color){0, 0, 0};
        }
      }
      
      
      ledStrip.write(colors, LED_COUNT); 
    
      delay(delayvalue);
      
}

When I disconnect the computer and connect Arduino to an external power supply, it appears that the readings I'm getting from analogRead are nonsense.

Could it be some sort of GND issue?

Yes it could.
It could also be a power supply problem, a regulator problem or a decoupling problem.

This is a hardware problem so post your schematic if you want help.

Thanks Mike

I’m not very good at drawing schematics.

Can you make sense from the attached image?

Note that power input for Arduino, LEDs and voltage reading are from a single DC power source. Namely a 58F ultracapacitor. Voltage input is from 12-15v

I’m pretty sure that because everything is running on the same ungrounded power source that there is no reference to earth. However, soon as the laptop is connected, it works perfectly (with or without the external power supply attached)

Thanks
Colin

Can you make sense from the attached image?

You have to attach it using "Attachments and other options".

Can you make sense from the attached image?

Quite frankly no.
Have you clicked on it? It is stupidly large, please resize it to no more than 1000 pixels in the largest dimension.

Is that the configuration that works or the one that doesn't.

Hint,
there is little point posting something that works and asking about the configuration that doesn't.

I'm not very good at drawing schematics.

If you can't draw a schematic then how on earth can you build it?

Is that any better?

This is the circuit that does not work!

Thanks for your patience mike, seems like i’m testing it.

Cheers
Colin

You say you are using A0 for the input yet there is no wire going to that pin. It looks like A5 that has something on it.

You have a USB hub but I can't see where it is getting its power from?
Are you sure the external power supply an provide the current?

What do you measure going into the analogue input from your multimeter?

Do you have a schematic of what you are trying to do?

Are you saying that just plugging the USB cable into the computer rather than the hub gets it to work?