Skip to main content
Added non-blocking example.
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

Your code compiles, but as Gerben said, the printf won't do anything in its current state, as no output handlers are defined for it. Why go to all that trouble when you can do this?

void setup() 
  {
  Serial.begin (115200);
  }

void loop() 
  {
  uint16_t adc_value;
  adc_value = analogRead (0);
  Serial.println (adc_value);
  delay (100);  // stop output spamming your monitor
  }

You could conceivably make printf work by following the tutorial here: http://playground.arduino.cc/Main/Printf

However I don't really see the point, when what I posted is much easier.


If performance is an issue, you might want to use the "advanced" code.

That "advanced" code above doesn't give you any more performance, because is still blocks. You can do a non-blocking version easily and readably enough:

const byte adcPin = 0;  // A0
  
bool working;
  
void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  ADCSRA =  bit (ADEN);   // turn ADC on
  ADCSRA |= bit (ADPS0) |  bit (ADPS1) | bit (ADPS2);  // Prescaler of 128
  ADMUX =   bit (REFS0) | (adcPin & 0x07);  // AVcc   
  }  // end of setup

void loop () 
  { 
  if (!working)
    {
    bitSet (ADCSRA, ADSC);  // start a conversion
    working = true;
    }
    
  // the ADC clears the bit when done
  if (bit_is_clear(ADCSRA, ADSC))
    {
    int value = ADC;  // read result
    working = false;
    Serial.println (value);
    delay (500);  
    }
    
  // do other stuff here

  }  // end of loop  

Now that code (which admittedly fiddles with the registers) doesn't block so you could be doing other things during the conversion. I don't see any reason you wouldn't use that on a commercial product.

See ADC conversion on the Arduino (analogRead) for more details.

Your code compiles, but as Gerben said, the printf won't do anything in its current state, as no output handlers are defined for it. Why go to all that trouble when you can do this?

void setup() 
  {
  Serial.begin (115200);
  }

void loop() 
  {
  uint16_t adc_value;
  adc_value = analogRead (0);
  Serial.println (adc_value);
  delay (100);  // stop output spamming your monitor
  }

You could conceivably make printf work by following the tutorial here: http://playground.arduino.cc/Main/Printf

However I don't really see the point, when what I posted is much easier.

Your code compiles, but as Gerben said, the printf won't do anything in its current state, as no output handlers are defined for it. Why go to all that trouble when you can do this?

void setup() 
  {
  Serial.begin (115200);
  }

void loop() 
  {
  uint16_t adc_value;
  adc_value = analogRead (0);
  Serial.println (adc_value);
  delay (100);  // stop output spamming your monitor
  }

You could conceivably make printf work by following the tutorial here: http://playground.arduino.cc/Main/Printf

However I don't really see the point, when what I posted is much easier.


If performance is an issue, you might want to use the "advanced" code.

That "advanced" code above doesn't give you any more performance, because is still blocks. You can do a non-blocking version easily and readably enough:

const byte adcPin = 0;  // A0
  
bool working;
  
void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  ADCSRA =  bit (ADEN);   // turn ADC on
  ADCSRA |= bit (ADPS0) |  bit (ADPS1) | bit (ADPS2);  // Prescaler of 128
  ADMUX =   bit (REFS0) | (adcPin & 0x07);  // AVcc   
  }  // end of setup

void loop () 
  { 
  if (!working)
    {
    bitSet (ADCSRA, ADSC);  // start a conversion
    working = true;
    }
    
  // the ADC clears the bit when done
  if (bit_is_clear(ADCSRA, ADSC))
    {
    int value = ADC;  // read result
    working = false;
    Serial.println (value);
    delay (500);  
    }
    
  // do other stuff here

  }  // end of loop  

Now that code (which admittedly fiddles with the registers) doesn't block so you could be doing other things during the conversion. I don't see any reason you wouldn't use that on a commercial product.

See ADC conversion on the Arduino (analogRead) for more details.

Added link to printf tutorial.
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

Your code compiles, but as Gerben said, the printf won't do anything in its current state, as no output handlers are defined for it. Why go to all that trouble when you can do this?

void setup() 
  {
  Serial.begin (115200);
  }

void loop() 
  {
  uint16_t adc_value;
  adc_value = analogRead (0);
  Serial.println (adc_value);
  delay (100);  // stop output spamming your monitor
  }

You could conceivably make printf work by following the tutorial here: http://playground.arduino.cc/Main/Printf

However I don't really see the point, when what I posted is much easier.

Your code compiles, but as Gerben said, the printf won't do anything in its current state, as no output handlers are defined for it. Why go to all that trouble when you can do this?

void setup() 
  {
  Serial.begin (115200);
  }

void loop() 
  {
  uint16_t adc_value;
  adc_value = analogRead (0);
  Serial.println (adc_value);
  delay (100);  // stop output spamming your monitor
  }

Your code compiles, but as Gerben said, the printf won't do anything in its current state, as no output handlers are defined for it. Why go to all that trouble when you can do this?

void setup() 
  {
  Serial.begin (115200);
  }

void loop() 
  {
  uint16_t adc_value;
  adc_value = analogRead (0);
  Serial.println (adc_value);
  delay (100);  // stop output spamming your monitor
  }

You could conceivably make printf work by following the tutorial here: http://playground.arduino.cc/Main/Printf

However I don't really see the point, when what I posted is much easier.

Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

Your code compiles, but as Gerben said, the printf won't do anything in its current state, as no output handlers are defined for it. Why go to all that trouble when you can do this?

void setup() 
  {
  Serial.begin (115200);
  }

void loop() 
  {
  uint16_t adc_value;
  adc_value = analogRead (0);
  Serial.println (adc_value);
  delay (100);  // stop output spamming your monitor
  }