Sampling voltage with Teensy 4.1

Colorado

New member
Hello all,

Hopeful someone can give me some advice on what I'm doing wrong. I'm attempting to sample voltage from pins 24 and 41. The pins are receiving 2x AA batteries as inputs but I'm never able to get a consistent reading on when the batteries are low. These batteries are also used to power on the board when not connected to a computer through USB via a boost converter. Currently I'm trying to sample the voltage from the batteries every minute.

#include <TimeLib.h>
int sampleVoltage;
int verifyVoltage;
time_t t = 0;
void setup() {
Serial.begin(9600);
while (!Serial && millis() < 5000) {
// wait for serial port to connect.
}
pinMode(24, INPUT_DISABLE);
pinMode(41, INPUT_DISABLE);
t = now();
}
void loop() {
if (minute(now()) - minute(t) >= 1){
sampleVoltage = analogRead(A17); //read pin 41
verifyVoltage = analogRead(A10);
Serial.print("Sample voltage = ");
Serial.println(sampleVoltage);
Serial.print("Verify voltage = ");
Serial.println(verifyVoltage);
t = now();
}
}
 
I can't comment on why your analog inputs might be noisy, but let's assume they are. In that case, doing a single analogRead() once per minute is not going to provide a steady result. You could modify setup() to call analogReadAverageing(32) so that each call to analogRead(pin) returns an average of 32 readings. Or, since you are measuring a voltage that should change very slowly, you could do your own averaging over a longer period of time.
 
Blind guess, perhaps you're using resistor dividers with high value resistors (over 10K) because you want to avoid wasting power in the resistors?

The ADC inputs want fairly low impedance drive. With pots, usually 10K is the highest resistance which gives good results.
 
As a quick test, I connected a similar battery to A10 using two 10K resistors, and the 3.3V power to pin A17 also using two 10K resistors.

1711976947396.png


I ran your program for several minutes. This is the result I see in the serial monitor window (Arduino IDE 1.8.19)

1711976983373.png
 
Blind guess, perhaps you're using resistor dividers with high value resistors (over 10K) because you want to avoid wasting power in the resistors?

The ADC inputs want fairly low impedance drive. With pots, usually 10K is the highest resistance which gives good results.

Paul I am not using any resistors since I was under the impression that feeding less than 3.3 V to any analog pin was acceptable. Currently I'm not doing anything with the 3.3 volt pins on my board. The boost converter I'm using is connected to the 5V pin to board the power without issue. I'm heading to work fairly soon but I can make a diagram if needs be when I get back but your layout without resistors would be very similar to mine in regards to pin A17.

Perhaps I need to be using resistors in my circuits for voltage verification? In practice I shouldn't really need to verify the voltage but I was trying to do things from a sanity check to see what is happening.
 
If the voltage is always less than 3.3V (even when the batteries are fresh) no resistors are needed. I simply made a guess, since you didn't give any details about how your hardware was connected. I could try again, but I still don't understand why 2 analog pins are used. Are you just connecting the same thing to both? Please give me a clear picture of how to connect the hardware and I can give another try.
 
If the voltage is always less than 3.3V (even when the batteries are fresh) no resistors are needed. I simply made a guess, since you didn't give any details about how your hardware was connected. I could try again, but I still don't understand why 2 analog pins are used. Are you just connecting the same thing to both? Please give me a clear picture of how to connect the hardware and I can give another try.

Two double A batteries even when fresh should result in less than 3.3 V that I can see. (I hope!) I've removed everything else on my board/software until I can sort this issue out.

teensy.png


The only reason I used two analog pins was to see if there was something wrong on one of the analog pins. Yes I was connecting it to the same thing (hence the names I used). Hopefully my drawing makes some sense and hopefully explains what I'm doing wrong.
 
I ran it again with the battery connected directly to A10 and A17. Here's what I see in the serial monitor.

Code:
Sample voltage = 979
Verify voltage = 979
Sample voltage = 979
Verify voltage = 978
Sample voltage = 979
Verify voltage = 979
Sample voltage = 979
Verify voltage = 979
Sample voltage = 979
Verify voltage = 979
Sample voltage = 979
Verify voltage = 979
Sample voltage = 979
Verify voltage = 978

Here's another photo, so you can see exactly how I tested.

1711998636744.png
 
I ran it again with the battery connected directly to A10 and A17. Here's what I see in the serial monitor.

Code:
Sample voltage = 979
Verify voltage = 979
Sample voltage = 979
Verify voltage = 978
Sample voltage = 979
Verify voltage = 979
Sample voltage = 979
Verify voltage = 979
Sample voltage = 979
Verify voltage = 979
Sample voltage = 979
Verify voltage = 979
Sample voltage = 979
Verify voltage = 978

Here's another photo, so you can see exactly how I tested.

View attachment 33907

I disconnected everything and re-connected everything and it appears to be working just fine now. So i'm lost as to what the problem was. I tried this on two different boards so sufficient to say I'm flabbergasted as to what went wrong.
 
So i'm lost as to what the problem was.
It may well be a worn-out or low quality breadboard.
Breadboards vary a lot in quality. A lot of chinese breadboards are very cheap but low quality - you get what you pay for!
Two brands stand out:
1. BPS [BusBoard Prototype Systems]. I have several BB400's in use. They even have a datasheet. Costs around $8-$10. Make sure the breadboard has "BusBoard.com" printed on. Paul Stoffregen also uses them, see the pictures above.
2. Assembly Specialist. They have been manufacturing the original 3M breadboard. Higher price than Busboard. Lots of fake copies on the Internet.

More reading here.

Paul
 
Indeed many years ago I tested almost every breadboard I could find, about 40 different models (but many were probably the same with different packaging).

BPS and Twin Industries were by far the best quality. That's why PJRC sells the BPS breadboard (though the photo on the website is still the Twin Industries model).

Even worse that low quality breadboards are the special breadboard wires so many vendors sell. Their quality is terrible. The wire is incredibly thin and prone to breaking. Even when not broken, some of them measure on the scale of 1 ohm resistance.

In the kits we sell, and in all the photos you'll see from me, good quality #22 solid wire is used. Together with the BPS or Twin Industries breadboard, it makes a huge difference in reliability... which translates into saving a lot of time not having to diagnose faulty wires while also struggling to figure out how to wire up the circuit.
 
Back
Top