10 years, 2 months ago.

Analog read to serial port

Hi,

I try to read an analog value from pin 20 and send it to the serial port.

My code:

  1. include "mbed.h"

AnalogIn sensor(p20); Serial pc(USBTX, USBRX); tx, rx

int main() { while(1) { pc.printf(sensor); } }

I always get this error: "No suitable conversion function from "mbed::AnalogIn" to "const char *" exists in "main.cpp", Line: 8, Col: 19"

Can somebody help me?

1 Answer

10 years, 2 months ago.

use:

pc.printf("Value = %f\r\n", sensor.read());

See also: http://www.cplusplus.com/reference/cstdio/printf/

That code obviously first prints Value =. Then you tell it to print a floating point (AnalogIn returns a float), followed by carriage return and new line (if they are both required depends on your serial terminal settings, but generally you want a new line). Then the next argument is the value it has to place for that floating point, which is sensor.read(). (Generally you can also use sensor, but in this case the code doesn't know if it should use the address of sensor, or the value read).

Accepted Answer