 # Converting Analogue input to real value

Hi all,

I am going through the demos using the sparkfun inventors kit and am on the twisting potentiometers one...

I am using this code

/* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor

This example code is in the public domain. */

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

void loop() { int sensorValue = analogRead(A0); Serial.println(sensorValue); }

When I am turning the pot I am seeing 0 - 1023 as I should be on the serial monitor

What i would like to do it convert this value to the range of a PH meter of which has a range of 0 - 14

As I see it 1023 / 14 = 73.07 bits per ph point

I am wondering how I can transpose this so that when the pot is in the middle and at 512 or 2.5V I would see 7.0 on the serial monitor....I would like it as #.# rather than #.###### etc so I think it needs to be rounded?

I am a noob as you would of gathered but I am trying to get to grips with analogue inputs so I can read 0.0 - 14.0 PH, 0.0 - 10.0 conductivity, turn on various relays for fixed times and trigger the digital inputs

Thanks

Take a look at the map() and constrain() functions. http://arduino.cc/en/Reference/Map

CrossRoads: Take a look at the map() and constrain() functions. http://arduino.cc/en/Reference/Map

Thanks for your help. I have tried using the below code

/* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor

This example code is in the public domain. */

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

void loop() { int val = analogRead(0); val = map(val, 0, 1023, 0.0, 14.0); Serial.println(val); }

I am trying to get it to use one decimal place i.e if it is on 0 then it would real 0.0 if it is half way across then it would read 7.0 I would like the second decimal place rounded off so if it reads 8.16 then it would read 8.2 if it where to read 10.5 then it would read 10.0 etc

The above is working fine but it only reads 1,2,3,4,5,6,7,8,9,10,11,12,12,14 without the decimal place

Thanks

it only reads 1,2,3,4,5,6,7,8,9,10,11,12,12,14 without the decimal place

That’s because you are working with integers.

dxw00d:

it only reads 1,2,3,4,5,6,7,8,9,10,11,12,12,14 without the decimal place

That's because you are working with integers.

ok can you explain to me how to do it bearing in mind i am a bacon

Something like:

``````void loop() {
int val = analogRead(0);
val = map(val, 0, 1023, 0, 140);
float fval = (float)val/10.0;
Serial.println(fval,1);
}
``````

For simple scaling map() is unnecessary:

``````  Serial.println(analogRead(0) * 14.0 / 1024, 1);
``````

MarkT: For simple scaling map() is unnecessary:

``````  Serial.println(analogRead(0) * 14.0 / 1024, 1);
``````

Thanks for that...seems a lot easier to understand the simpler way (well sort of)

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

void loop() { int val = analogRead(0); Serial.println(analogRead(0) * 14.0 / 1024, 1);

can you explain what the 1 means at the end in

Serial.println(analogRead(0) * 14.0 / 1024, 1);

please

I intend eventually connecting a gsm module to this so upon sending the text message "PH" I am hoping it will reply with something like "PH = #.#"

How hard would you say this is to do? As I can see it in serial monitor I am asuming you point the GSM modem to a specific location and the modem translates the command to read from that location or similar?

Thanks

can you explain what the 1 means at the end in

Serial.println(analogRead(0) * 14.0 / 1024, 1);

please

You could look it up yourself - http://arduino.cc/en/Serial/Print

You could try using the float version of map (paste the function inbetween setup() and loop()):

``````void loop()
{

int raw = analogRead(analoginput);
float val = fmap(raw, 0, 1023, 0.0, 5.0);
Serial.println(val); //voltage
delay(1000);
}

float fmap(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
``````