int analogPin = A0; // potentiometer wiper (middle terminal) connected to analog pin 0
// outside leads to ground and +5V
float val = 0; // variable to store the value read
float volt = 0;
void setup() {
Serial.begin(9600); // setup serial
}
void loop() {
float volt = 0;
val = analogRead(A0);
volt = ((val*5.06)/1023); // read the 9600 times analog input pin A0 and take the average
Serial.print("V:");
Serial.println(volt,4);
}
This is the set-up (the DRV8825 is not hooked up to anything, just from another project):
Those wires looks to long and not screened will pick up a lot of noise.
Disconnect everything from A0.
Use 2 x 10k resistors (or the closest value) from 5v to A0 and GND to A0 , you should read a constant 2.5V .
Use only the PC Usb power.
Also do a Serial.println(val); to see the actual raw ADC value and how much that varies.
This is a DRV8825 microcontroller (I mentioned this in post #22) with a 100uF capacitor for a stepper motor from another project. I will remove them from the breadboard to avoid confusion.
So why do you still have the 100 + 150 resistors? Have you changed your program? Did you read about star points?
If you make the suggested changes - and particularly change your wiring - you will get more sensible readings. You dont need the breadboard and its probably the main cause of your problems. I'd suggest you use connector strip like this
Changed the set-up. Added a 1uF capacitor as well as removed the breadboard used the connector strips as you suggested. Additionally I used the 1.1 reference voltage and an 56 Ohms resistor. I measured the reference voltage using a multimeter and instead of 1.1V it was 1.08V hence I changed this value in the code below.
The arduino readings seem the be more constant, however they are not (yet) in line with the multimeter. The value measured is the arduino is lower then the what the multimeter is reading. The higher the voltage that is measured, the higher the difference between the arduino and the multimeter therefore I should calibrate it I think.
With the code beneath:
//Analog input and pin assignments
const byte V1Pin = 1; // use will use analog pin 1 for voltage 1
int reading; //the value we read from the ADC
// sampling parameters
int nSamp = 16; // Number of Samples to take for each reading - best if it's a power of two.
int interval = 7; // milliseconds interval between successive readings
unsigned long sum1 = 0; // the total of nSamp readings from the ADC
//calculating voltages
int result; // calculated value of measured voltage in millivolts
//float vScale=5; //choose the right scale to suit the reference you have chosen.
//float vScale=3.3;
//float vScale=2.56;
float vScale=1.08; // originally 1.1, however measured with a multimeter that Aref = 1.08V
//*** If you calibrate the system you can adjust the value of vScale to give more accurate readings.
void setup()
{
Serial.begin(9600);
analogReference(INTERNAL); // program is being tested on a 5V Arduino Micro Pro - 32U4 chip so this will be 5.0V
//*** The chip on your Arduino - depending on type - is provided with SOME of the following reference voltages
//DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
//INTERNAL: a built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 and 32U4 chip boards.
//EXTERNAL: the voltage applied to the AREF pin. The Arduino Micro Pro does not have a pin to let you do this
vScale = vScale * 1000/1024; //scale the reading to read in millivolts
reading = analogRead(V1Pin); // dummy read to settle ADC Mux
delay(5);
}
void loop()
{
sum1 = 0; //ready to start adding values
//add up nSamp successive readings;
for (byte count = 0; count < nSamp; count++ )
{
reading = analogRead(V1Pin); // actual read
sum1 = sum1 + reading;
delay(interval);
}
// we print reading values to help with calibration
Serial.print("Raw");
Serial.print(reading);
Serial.print("Total of ");
Serial.print(nSamp);
Serial.print(" readings is: ");
Serial.println(sum1);
result = convReadings(sum1, nSamp, vScale);
Serial.print("Voltage is ");
Serial.print(result);
Serial.println(" millivolts.");
delay(2000);
}
//
// Calculate average sensor reading, convert to voltage
//
int convReadings(int sum, int number, float scale)
{
sum = sum + (number >>1); // add a correction of 0.5 of the reading - see notes
float mV = sum * scale; // mV = sum * (Vref * 1000 / 1024 )
mV = mV / number; //divide by number of readings last to keep precision high
return ((int) mV); //return the result as an integer as decimal point values arent valid
// (int) is a "cast" to change a float variable to an integer. Values after the decimal point are "cut off" so float 1.93 becomes int 1
}
I dont think you can calibrate it like that; you will still have possible offset and gain errors. Check the reading you get with A0 grounded to calibrate the offset;; then apply a known voltage near 1.000V to calibrate the range. (you can use a pot and your voltmeter but dont exceeed 1.0V