Decimal Points with Map – Nigel Moody
This question comes from Nigel Moody. It comes from this lesson:
- Arduino Course for Absolute Beginners, 2nd Edition
- Control Structures
The Question
“Hello Micheal,
Enjoying your course, and finding I am taking it in a little slower now, but I am persevering.
I have a pressure sensor from work I have been trying to test. I have been fairly successful with the readings but i am stuck getting a final reading in Bar. The sensor works on 4-20mA, I have managed to get readings from that to 0 – 5 volts.
Read the sensor value from 0 – 1023
Read the mA from 0 – 20.
I appear to be stuck getting a decimal reading in Bar, so in short 4mA should be 0 Bar.
Using the map function I can get the conversion but I cannot get it to read a decimal, it will only jump up in whole increments. I have searched and tried different variation but cannot get the result I am after.
Would be pleased for a little help or a nudge in the correct direction.”/* To test a 4-20mA pressure sensor. voltage range 8-30 volt dc 2 wire operation */ //set analogPin input pin const int analogPin = A0; // int sensorValue = 0; float bar = 0.0; void setup() { // put your setup code here, to run once: //Start serial monitor baud rate. Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: //variable sensorValue with analogRead sensorValue = analogRead(analogPin); //convert 0 - 1023 to 5 volts and make a float to get a decimal //output. float voltage = sensorValue * (5.0 / 1023.0); // Serial print voltage output 0 - 5 volts Serial.print("Voltage "); Serial.println(voltage); // // Serial print senosorValue 0 - 1023 Serial.print("sensorValue "); Serial.println(sensorValue); // //Serial print mA output with conversion in decimal float mA = sensorValue * (20.0 / 1023.0); Serial.print("mA "); Serial.println(mA); // bar = map (mA,4,20,0,6); bar = map (voltage, 1.0, 5.0, 0.0, 6.0); // bar = map (voltage, 1, 5, 0, 6); // float bar = map (mA, 0.0, 20.0, -1.0, 6.0); Serial.print("BAR "); Serial.println(bar); /* * Problem getting a decimal output for the BAR reading. * The sensor reads from 4-20mA = 0-6Bar if the reading * is less than 4mA then it brings up a fail to say the * wire could be broken, I will attempt that code when * I have the correct output. */ delay(500); }
Your Challenge
Solution
Here are my initial thoughts:
Nigel came back with a solution, here are some more thoughts:
Your Thoughts
What do you think? Did you try writing your own Map function?

Yep happy to say I eventually got it sussed yippee!:
The Sensor Value is: 1023 The Voltage is: 5.0 The Current is 20mA The Pressure is 6Bar
The Sens…
etc.
Sweet! Nice work Robert!
Forgot:
Thanks, Michael and Nigel!
This was an extremely helpful problem, debug, and solution. I am relatively new to programming but it’s pretty obvious that mapping or “re-scaling” is a necessary tool for reading and controlling inputs and outputs in the real world. I have NO DOUBT I will be referencing both Nigel and Michael’s solutions in the future for my projects. Thank you both.
Alan