Is there a simple and complete example code to utilize low noise analog-digital conversion of Arduino?
This forum has an example at http://forum.arduino.cc/index.php?topic=45949.0;wap2
but I got strange values with it. The tests were like this:
There is a voltage divider with 10k and 10k ohm legs, one 0.1 uF capacitor.(image as attachment). This simple program gives values 512 or 513 all the time as it should.
void setup(){
Serial.begin(115200);
analogReference( DEFAULT);
}
void loop(){
int sensorvalue = analogRead(A5);
Serial.println(sensorvalue, DEC);
delay(500);
}
The copy of the noise reduction program, with some necessary additonal lines is here:
/* from http://forum.arduino.cc/index.php/topic,38119.0.html#14
sleep while doing adc
*/
#include <avr/sleep.h>
ISR(ADC_vect){ // else application is reset
}
int rawAnalogReadWithSleep( void )
{
// Generate an interrupt when the conversion is finished
ADCSRA |= _BV( ADIE );
// Enable Noise Reduction Sleep Mode
set_sleep_mode( SLEEP_MODE_ADC );
sleep_enable();
// Any interrupt will wake the processor including the millis interrupt so we have to...
// Loop until the conversion is finished
do
{
// The following line of code is only important on the second pass. For the first pass it has no effect.
// Ensure interrupts are enabled before sleeping
sei();
// Sleep (MUST be called immediately after sei)
sleep_cpu();
// Checking the conversion status has to be done with interrupts disabled to avoid a race condition
// Disable interrupts so the while below is performed without interruption
cli();
}
// Conversion finished? If not, loop.
while( ( (ADCSRA & (1<<ADSC)) != 0 ) );
// No more sleeping
sleep_disable();
// Enable interrupts
sei();
// The Arduino core does not expect an interrupt when a conversion completes so turn interrupts off
ADCSRA &= ~ _BV( ADIE );
// Return the conversion result
return( ADC );
}
void setup(){
const byte channel = 5; // A5
Serial.begin(115200);
analogReference( DEFAULT);
Serial.println(F("Read analog pin after sleeping adc"));
// ref is DEFAULT
ADMUX = (0 << REFS1) | (1 << REFS0) | (0 << ADLAR) | (0x07 | channel);
}
void loop(){
int sensorvalue = rawAnalogReadWithSleep();
Serial.println(sensorvalue, DEC);
delay(500);
}
The above noise reduction program gives results like this:
367
348
335
323
315
308
305
301
299
297
295
296
296
294
295
294
295
293
294
293
294
294
295
294
295
294
295
295
295
294
295
294
The list continues with values at about 294 ±1. The expected value was 512 ±1.
Can you pinpoint an error here or give a link to another example?
PS. There are black shadows in the image.