Jump to content

NLCbanner2024.jpg.2478be509670e60c2d6efd04834b8b47.jpg

Digital Oscilloscope for Testing & Debugging Astro Remote Controls


Gina

Recommended Posts

  • Replies 180
  • Created
  • Last Reply

I don't think it's that because the LED test sketch runs fine.  In fact that runs at well below the lowest voltage the Li-Ion cell will reach.  eg. I had it running the LED test sketch on 3v and running the oscilloscope as well through the up converter - all working perfectly.

It may be that the A0 input is at the same voltage as Vcc - straight off the cell (or bench PSU for testing).  With Vcc at 5v the LEDs show the right cell charge status using my bench PSU to simulate a discharging Li-Ion cell.  I haven't tried setting the thresholds lower and running Vcc at 3.7v and A0 through a voltage divider - that might work.  Alternatively, I guess I could use the up converter which supplies 8v to the oscilloscope and drop the volts down to 5v or a little less for Vcc.

One disadvantage would be that the current drawn by the voltage display circuit would double because the up converter doubles the voltage and with the same actual load current the supply current to the up converter is doubled.  OTOH the LED that's on will only draw 10mA and the drain of the AVR is negligible so the total will only go up to 20mA or so and is still low compared with the 300mA needed by the oscilloscope.

Link to comment
Share on other sites

Gina, You could be having trouble with the oscillator not being guaranteed to work reliably at 16Mhz with the voltage of a single LIPO. 328P is only reliable to 8Mhz at 3.3 Volts.

 

Martyn.

Link to comment
Share on other sites

Thanks for you reply Martyn but as I said above, the LED test sketch works fine down to 3v surely that wouldn't be the case if the oscillator wasn't working properly - the LEDs come on for a second each as intended.

Link to comment
Share on other sites

Think I may have found the problem having read the ATMegaxxx datasheet.  There is a pin labelled AVcc that supplies the ADC and the datasheet says this should be connected to Vcc, either directly or through a LPF.  Well, I haven't connected it to anything :eek:

Link to comment
Share on other sites

Further reading has turned up another possibility.  The Aref voltage is taken from the +5v by default which I presume is Vcc.  So if Vcc is not 5v the ADC will not work properly.  The answer would seem to be to use an external reference voltage.  Clearly, the analogue input voltage must be below this as the analogRead() function gives the ratio of analogue input to Aref x 1023 (or 1024). 

I think I shall try using a 4.7v zener diode fed from the up converter.  Or even a voltage divider as the up converter provides a regulated voltage.

Link to comment
Share on other sites

I've always found the AVR A2Ds pretty reliable.

You do need to make sure they are in the right voltage reference mode.

You also need a small cap from Aref to ground as well as joining AVcc to Vcc.

If you use the internal 1V1 reference you will have to divide the cell voltage by ~4 with two resistors but need not worry about changes in AVcc.

 

Link to comment
Share on other sites

Quote

The reference voltage for the ADC (VREF) indicates the conversion range for the ADC. Single ended channels
that exceed VREF will result in codes close to 0x3FF. VREF can be selected as either AVCC, internal 1.1V
reference, or external AREF pin.

Make sure it isn't set up[ to use Vref as the reference if the pin is unconnected...

Link to comment
Share on other sites

Sounds like the best solution is to use the internal 1V1 reference and divide the input.  The maximum cell voltage should be 4.2v so 4:1 ratio should be ideal.  That would be a resistor ratio of 3:1.  A 3K3 and 1K would give 4.3:1 so 4.2 volts divide down to 0.977 which is 0.8879 of 1.1v giving a value of 908.  The required reference values will be :-

  • 4v = 865 
  • 3.8v = 822
  • 3.6v = 779
  • 3.5v = 757

All this assumes that the reference is exactly 1.1v otherwise calibration will be required.

Link to comment
Share on other sites

Run a quick test and the voltage levels seem about right.  Only thing is, the blue LED is still flashing.  Something silly, no doubt.

Link to comment
Share on other sites

Ran a test with the normal sketch but entered the val reading manually to turn on one LED at a time on the Arduino UNO and the blue LED flashes.  The yellow LED also flashes very slightly.  The blue LED was brighter than the others so I reduced the PWM value from 32 to 16 and it's now no brighter than the others but still flashes.  Now this is with 5v Vcc supplied from the USB port on my desktop.  No funny voltages.

I know the timing depends on going through reading the input value then going through the if...else table but I wouldn't have thought that would take anything like 100ms which seems to be the flicker rate.  I guess I could take "val" and only do the number test if it changes by more than so much.

Link to comment
Share on other sites

That stopped the flickering.  I guess the loop timing is what caused the flicker so needs changing a bit.  This is the code that I was using.

void loop() {
int val = analogRead(cellPin);
// turn all LEDs off
analogWrite(redPin,0);
analogWrite(bluePin,0);
analogWrite(greenPin,0);
analogWrite(yellowPin,0);
analogWrite(orangePin,0);
//
// Turn appropriate LED on
if (val > fullVal) analogWrite(bluePin,blueBrightness);
else if (val > goodVal) analogWrite(greenPin,greenBrightness);
else if (val > midVal) analogWrite(yellowPin,yellowBrightness);
else if (val > lowVal) analogWrite(orangePin,orangeBrightness);
else analogWrite(redPin,redBrightness);
}

I think I'll set a variable with the level then if that level changes I'll turn off the LED that was on and turn on the new one, rather than turning them all off and then testing for value and turning the appropriate one on.

Link to comment
Share on other sites

Here is the new sketch.

// Sketch for ATMega328P-PU for Digital Oscilloscope
//
//Looking at discharge curves for Lithium Ion cells and thinking about the voltage levels for each LED.  
//I'm thinking of the following voltage levels :-
//
//    D11 - Blue - Above 4.0v - Fully charged
//    D10 - Green - 3.8-4.0v - Good
//     D9 - Yellow - 3.6-3.8v - Half full
//     D6 - Orange - 3.5-3.6v - Nearly empty
//     D5 - Red - Below 3.5v - STOP, recharge now.
//
//  Cell positive connected to A0 via 3K3 and 1K voltage divider - ratio 4.3:1  ADC using internal 1.1v reference
//  LEDs on D5 D6 D9 D10 D11 - Red Orange Yellow Green Blue
//  LEDs have variable brightness using PWM therefore output with analogWrite(redPin,redBrightness); Brightness variables to be set on test
//
//  ***** Working part starts here *****
//
// Cell discharge voltage presets
int fullVal = 865;
int goodVal = 822;
int midVal = 779;
int lowVal = 757;
//
// LED brightness presets
int redBrightness = 64;
int orangeBrightness = 32;
int yellowBrightness = 50;
int greenBrightness = 255;
int blueBrightness = 16;
//
int cellPin = A0;
int redPin = 5;
int orangePin = 6;
int yellowPin = 9;
int greenPin = 10;
int bluePin = 11;
//
int chargeLevel = 0;
int oldLevel = 0;
//
//
void setup() {
  analogReference(INTERNAL);  // Use internal 1.1v reference for ADC
  pinMode(cellPin,INPUT);   //  cell voltage sensor
  pinMode(redPin, OUTPUT);
  pinMode(orangePin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
// 
// Turn all LEDs off
  analogWrite(redPin,0);
  analogWrite(orangePin,0);
  analogWrite(yellowPin,0);
  analogWrite(greenPin,0);
  analogWrite(bluePin,0);  
}
//
void loop() {
int val = analogRead(cellPin);
//
// set chargeLevel as appropriate depending on val
if (val > fullVal) chargeLevel = 4;
else if (val > goodVal) chargeLevel =3;
else if (val > midVal) chargeLevel = 2;
else if (val > lowVal) chargeLevel = 1;
else chargeLevel = 0;
//
// Any change??
if (chargeLevel == oldLevel) return;  //  no change so do nothing
//
// chargeLevel has changed so change LED
// turn old LED off
switch (oldLevel) {
    case 0:  analogWrite(redPin,0); break;
    case 1:  analogWrite(orangePin,0); break;
    case 2:  analogWrite(yellowPin,0); break;
    case 3:  analogWrite(greenPin,0); break;
    case 4:  analogWrite(bluePin,0); break;
}
//
// and turn new LED on
switch (chargeLevel) {
    case 0: analogWrite(redPin,redBrightness); break;
    case 1: analogWrite(orangePin,orangeBrightness); break;
    case 2: analogWrite(yellowPin,yellowBrightness); break;
    case 3: analogWrite(greenPin,greenBrightness); break;
    case 4: analogWrite(bluePin,blueBrightness); break;
  }
}

 

Link to comment
Share on other sites

Testing the latest sketch with the ATMega328P-PU in my battery board I get a strange result.  With my bench PSU connected to the battery box terminals instead of the cell and gradually increasing voltage, the red LED doesn't light, the orange comes on at about 3.3v (too low) then the yellow comes on but the orange stays on and then the green comes on with both the orange and yellow still on.  Further increase and it stays the same - the blue LED never comes on even at 5v.

As always (it seems) I have a "funny" in my project!!!  Urrgghh...

Link to comment
Share on other sites

I guess the next stage would be to go back to the Arduino UNO and connect variable voltage to the A0 pin while running off USB and see what happens with that.  I think I'll need to add in a voltage divider as the voltage control of my small bench supply is a bit coarse at that low voltage viz. 0 to 1.1v or actually 0.977v representing the highest test level of 4.2v.  My DMM can easily handle the low voltage with a resolution of 1mv on the 2v range.

Link to comment
Share on other sites

Have 8K2 and 680 resistors connected as voltage divider and attached to my bench PSU, connected DMM to junction and PSU -ve then on to the A0 and Gnd on the UNO.  With everything on the table rather than next to my PC I plugged the USB cable into the USB supply on my bench PSU (handy feature and one reason I bought this little bench PSU to augment my toolkit).  Set PSU to 15v range and gradually advanced volts.  Same result as with my battery board.  Furthermore, when reducing the volts the LEDs stayed on. 

Link to comment
Share on other sites

Decided to check if this was a hardware issue - maybe I'd damaged the chip in some way - so I took another UNO, uploaded the sketch to it and connected LEDs and everything.  Same result.  It would seem to be the sketch then but darned if I can see anything wrong with it!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.