I’m using the standard analogInput.ino example with an ATtiny85 chip, to read a voltage from an analog input pin. After programming the chip, and moving it to a bare breadboard, the ADC appears to be saturating at 3.25 volts, giving a maximum value of 1025. The ADMUX io register is read as 0x02, so it should be using the Vcc voltage reference, but I cant read any voltages above 3.25 volts. I’m using another pin to send serial print output to see what I’m getting, but I’m only connecting to the RxD pin on the serial adapter, so I dont think its backloading the ADC. other than that, there are no other components on the breadboard, which is powered by a well regulated 5V power supply. What might be causing the ADC read voltage to be saturated at 3.25V?
1025? Really? From a 10 bit ADC?
Sketch, schematic, and wiring pictures, please.
You misunderstood. The A/D does NOT return a voltage.
It only returns an x/1024th value of it's reference voltage.
"Voltage" is what humans make of it with code.
If your code returns the wrong voltage, then your code must be wrong.
What A/D value do you get when you connect the analogue input to VCC.
Leo..
The standard analogInput.ino example in the IDE, which you have mentioned, is a remarkably odd as an example of using the ADC.
It prints nothing out and simply controls the ontime of a led depending on the voltage on pin A0.
What you appear to be doing is at a completely different level so you'd better show your code and schematic.
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on and off a light emitting diode(LED) connected to digital pin 13.
The amount of time the LED will be on and off depends on the value obtained
by analogRead().
The circuit:
- potentiometer
center pin of the potentiometer to the analog input 0
one side pin (either one) to ground
the other side pin to +5V
- LED
anode (long leg) attached to digital output 13 through 220 ohm resistor
cathode (short leg) attached to ground
- Note: because most Arduinos have a built-in LED attached to pin 13 on the
board, the LED is optional.
created by David Cuartielles
modified 30 Aug 2011
By Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInput
*/
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
This is the kind of thing we're looking for, by the way. Only yours should show a voltage of 3.25V giving a full scale ADC reading of 1023.
Looking forward to seeing your code and setup.
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
SSD1306AsciiWire oled;
void setup() {
Wire.begin();
oled.begin(&Adafruit128x64, 0x3C);
oled.setFont(Adafruit5x7);
oled.set2X();
}
int oldVal = -1;
void loop() {
int val = analogRead(A3);
if( val != oldVal ) {
oled.clear();
oled.print("A3=");
oled.print(val);
oldVal = val;
}
delay(1000);
}
(ATtiny core, attinyx5, 8MHz internal, other options left at default values)
You of course, disabled the reset function with its associated weak pullup that is the default function of pin1, where ADC0 also resides, when you programmed the ATtiny85, correct? And you have a HV programmer to re-enable the reset functionality afterwards?
I think he is OK. That looks like: Arduino: A2, package pin: 3 , PB4
Shrug; then they're not doing what they said were doing, which was using the standard analogInput.ino example.
Which is why showing the actual sketch in use is pretty much mandatory, OP.
@danbecker75
1. For a fresh ATtiny85, Pin-1 is a RESET/-pin and at the same time the ADC0 channel is also acitive (range: 2.9 V to 5.0 V).
2. You can make the following setup (Fig-1) and verify the working of both RESETT/pin and ADC0 channel.
Figure-1:
3. Enter the following sketch into the flash memory of ATtiny85 using a programmer. If you wish, you can use Arduino UNO R3 as a Programmer (Section-19.2 of the attached file).
#include<Wire.h>
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //if 0x27 deos not work; try 0x3F (I2C Slave Address)
#define LED 1 //PB1
void setup()
{
Wire.begin();
pinMode(LED, OUTPUT);
analogReference(DEFAULT); //5 V
lcd.begin();//if does not work try lcd.init();
lcd.backlight();
lcd.setCursor(0, 0); //DigitPosition (0-15), LinePosition (0-1) 0 = Top Line
//----------------------
Wire.beginTransmission(0x27);
byte busStatus = Wire.endTransmission();
if (busStatus != 0)
{
while(true) //LED keeps flashing at 200-ms interval when LCD is not found
{
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
}
}
lcd.print("I2CLCD is found!");
delay(2000);
lcd.clear();
}
void loop()
{
lcd.setCursor(0, 0);
digitalWrite(LED, HIGH); //debug message to see MCU reset
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
//------------------
unsigned y = analogRead(A0);
float testVolt = (5.0 / 1023) * y;
lcd.print(testVolt, 1);
lcd.print(" V");
}
4. Turn the Pot to see a reading > 3.50 V on DVM. The MCU is out-of-reset and is active. The LED makes blinks. Check that reading of LCD and DVM are very close.
5. Turn the Pot to increase the reading of upto 5.00 V. Check that reading of LCD and DVM are very close.
6. Turn the Pot in the reverse direction to decrease the voltage at RESET/-pin and check that at about 2.80 V level, the MCU enters RESET/-state. This is observed by noticing that LED does not blink anymore.
7.
Ch-19 ATtiny85 MCULec (5).pdf (902.0 KB)
Ok. attached is sketch, schematic and test results graph. I used a slightly modified version of the standard example, by deleting the LED functionality. Didnt think that should make a difference. and I added the SendOnlySoftwareSerial so that I could see the actual results. I have left the reset pin floating in the breadboard after it was programmed. The graph shows the voltage I measured on the input to PB4 and the corresponding values returned by the analogRead() call.
The USB serial box is an off the shelf USB to TTL converter, that I can read on my pc from a terminal app. the TxD output of the converter is not connected. That is how I obtained the sensor readings. potentiometer is a 10K pot.
AnalogInput.ino (939 Bytes)
WaterLevelProtoGraph.pdf (33.5 KB)
Please post your code using code tags as described in https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code-and-common-code-problems instead of attaching it. It's small enough.
OK. So as you turn the potentiometer you see analog values from 0 through to 1023 in the serial console. Is that correct ?
If not, what do you see if you remove that volt meter ?
What is the resistance of the potentiometer between pins 1 and 3 when out of circuit ?
Are you sure VCC is 5volt.
Your measurements would make sense if VCC is 3.5volt.
Not sure why you want to convert to volt. Link says water level.
The logical way is to convert A/D values to water level, not to volt.
Is your pot good. We had a similar thread in the past where the user had soldered wires to the pot rivits, destroying the carbon track.
Leo..
yes, Vcc is from a well regulated 5V nominal supply, and measured a with multimeter as 5.13. you can see the pot is working, since I’m measuring the voltage from the wiper arm, and it varies as I dial the pot.
This is a prototype so that I can verify the measured voltages. once I fix that, the pot will be replaced by sensors that sit in water and vary resistance depending on water level. but I need to know how the measured ADC values correspond to actual voltages. and that isnt working as I expected. The sensor values would make sense if it was using a 3.25 Volt reference, but with the settings, I’d have expected the chip to be using a Vcc reference.
the pot is brand new, and right now just connected with clip leads, not soldered.
that is correct. removing the voltmeter does nothing to output to console. just continues to print the current value read. pot resistance is 7.94 Kohm.
my apologies for not posting correctly.
#include <SendOnlySoftwareSerial.h>
SendOnlySoftwareSerial mySerial(1); // Tx pin
int sensorPin = A2; // select the input pin for the potentiometer
//int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int i;
void setup() {
// declare the ledPin as an OUTPUT:
//pinMode(ledPin, OUTPUT);
mySerial.begin(9600);
}
void loop() {
// read the value from the sensor:
int admux = ADMUX;
mySerial.print("ADMUX=");
mySerial.println(admux);
sensorValue = analogRead(sensorPin);
mySerial.print(i++);
mySerial.print(" ");
mySerial.println(sensorValue);
// turn the ledPin on
//digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(2000);
// turn the ledPin off:
//digitalWrite(ledPin, LOW);
// stop the program for <sensorValue> milliseconds:
//delay(sensorValue);
}
OK. Then there is no problem as far as I see. 1023 represents Vcc which is 5v according to your diagram.
Scale that down accordingly for other voltages. For example, 2.5 volts at PB4 (A2) should show an analog reading of 512 analog units.
Why read and print ADMUX if you're changing it with analogRead(sensorPin)?
I guess the voltage graph wasnt clear. the V column represents the actual measured voltage applied to the attiny85. The second column “Value Breadboard” shows the sensor reading as returned by analogRead(). If you look at the data, yes, 5V does return 1023, but so does every voltage from 3.37 on up. so the readings saturate at 3.37, and you cant just scale it linearly from 5V. Thats the problem. 5V should show 1023, but 4 volts should return something less than 1023.
analogRead() only changes ADMUX to specify the pin being converted, if I understand that function correctly. I was only printing it on the console to confirm that the default reference voltage settings in the ADMUX register (REFS[2:0]) was correct and set to use Vcc as a voltage reference, and was not being modified by analogRead(), which should just return the 10 bit representation of the voltage being converted. If REFS was set to something other than 0, it could explain the voltage reading discrepancies.





