# How to map sensor analog output voltage

My sensor output voltage values ranges from 0.6 V - 3 V. How can I convert this voltage values to ppm in the range 0-1000? Upon exposure to CO gas: 0 ppm = 0.6 V , 500 ppm = 1.52 V and 1000 ppm = 3.01 V. Which means that the sensor cannot detect under 214 ppm (approx). Can you please help with mapping the sensor output values as ppm in the serial monitor? thnk u

``````// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);

// print out the value you read:
Serial.println(voltage);

}
``````
• What do you mean by "sensor cannot detect under 214 ppm"? Do you have the link to the sensor's data sheet? If you are using the MQ-7 sensor, are you using a standalone unit, or with a breakout board? Have you calibrated the sensor? – Fauzan Jul 24 '17 at 3:53
• link if you have a look at the data sheet, they said the analog output voltage is from 0.6-3V and sensor range is 0-1000. According to that, the sensor cannot read anything below 200ppm (0.6=20% of 3V). And I have calibrated the sensor and its giving me 1.52V for 500pm and 3.01 for 1000ppm. How can I print the ppm values equivalent of output voltages? – LD_959 Jul 24 '17 at 4:08
• Fyi: The value can be between 0 and 1023 so you want to divide by 1024, not 1023. – SDsolar Aug 6 '17 at 3:20

## 3 Answers

On page 4 of the datasheet,

Left side value of detection range (zero point) is corresponding to left side value of voltage output range,but right side value of detection(max detection value) is not corresponding to the right side value of voltage output range.

Thus, 0ppm would correspond to 0.6V, for CO. And 1000ppm does not necessarily correspond to 3V.

Assuming that 500ppm = 1.52V was obtained from the "inspection report", you now have two reference points, (0, 0.6) and (500, 1.52).

Further below on the same page, a formula for `N` is given, which, points towards a linear response of Vo with respect to ppm. Thus, you can use the `map()` function to do the linear mapping from voltage to ppm.

Note that `map()` only works with integers, so instead of mapping voltage (0.6 - 1.52) to ppm, you should directly map the analogRead value (123 - 311) to ppm to get better accuracy. Other wise, implement your own mapping using floats.

• So are suggesting that I use expression like this: ppm= map (voltage, 0.6, 3, 0, 1000)? Based on my code. Also with regards to the formula mention in pg.4, for example say my Vo=1.5V then if I do the math value of N=600ppm rather than 500ppm that I tested with. Can you please write down your version of mapping voltage to ppm? – LD_959 Jul 24 '17 at 5:30
• No. Use `map(voltage, 0.6, 1.52, 0, 500)`. But since `map()` uses integer math, better use `map(sensorValue, 123, 311, 0, 500)`. Note that `123 * (5.0 / 1023.0) = 0.6` and `311 * (5.0 / 1023.0) = 1.52`. – Fauzan Jul 24 '17 at 5:45
• Your mapping function is working. Thank you so much!!! – LD_959 Jul 25 '17 at 6:19

This Codes works for you :)

``````int SensorPin = A0;
int Readings;
int Min = 0;    // This is the Min reading of the sensor
int Max = 614; // This is the Max reading of the sensor
int Desired_Min = 0; // this is the desired number that you want converted with min reading of the sensor
int Desired_Max = 1000; // this is the desired number that you want converted with max reading of the sensor

void setup(){
Serial.begin(9600);
pinMode(SensorPin, INPUT);
}

void loop(){
Readings = analogRead(SensorPin);
Readings = map(Readings, Min , Max, Desired_Min, Desired_Max);
Serial.println("Mapped Value:"+Readings);

}
``````

I Strongly prefer you to note the min and max reading Value of your sensor with arduino and replace the 'Min' and 'Max' according to that in the programm....

In the main loop, `sensorValue` is assigned to store the raw analog value read from the potentiometer. Arduino has an `analogRead` range from 0 to 1023, and an `analogWrite` range only from 0 to 255, therefore the data from the potentiometer needs to be converted to fit into the smaller range before using it to dim the LED.

In order to convert this value, use a function called `map()`:

``````outputValue = map(sensorValue, 0, 1023, 0, 255);
``````

`outputValue` is assigned to equal the scaled value from the potentiometer. `map()` accepts five arguments: The value to be mapped, the low range and high values of the input data, and the low and high values for that data to be remapped to. In this case, the sensor data is mapped down from its original range of 0 to 1023 to 0 to 255.

The newly mapped sensor data is then output to the `analogOutPin` dimming or brightening the LED as the potentiometer is turned. Finally, both the raw and scaled sensor values are sent to the Arduino Software (IDE) serial monitor window, in a steady stream of data.

``````// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the Serial Monitor:
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);
}
``````

Few useful commands while mapping any analog output values:

• map()
• analogRead()
• analogWrite()
• serial()
• AnalogInput - Use a potentiometer to control the blinking of an LED.
• AnalogWriteMega - Fade 12 LEDs on and off, one by one, using an Arduino or Genuino Mega board.
• Calibration - Define a maximum and minimum for expected analog sensor values.

``````/*
Calibration

Demonstrates one technique for calibrating sensor input. The sensor readings
during the first five seconds of the sketch execution define the minimum and
maximum of expected values attached to the sensor pin.

The sensor minimum and maximum initial values may seem backwards. Initially,
you set the minimum high and listen for anything lower, saving it as the new
minimum. Likewise, you set the maximum low and listen for anything higher as
the new maximum.

The circuit:
- analog sensor (potentiometer will do) attached to analog input 0
- LED attached from digital pin 9 to ground

created 29 Oct 2008
by David A Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Calibration
*/

// These constants won't change:
const int sensorPin = A0;    // pin that the sensor is attached to
const int ledPin = 9;        // pin that the LED is attached to

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value

void setup() {
// turn on LED to signal the start of the calibration period:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);

// calibrate during the first five seconds
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);

// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

// signal the end of the calibration period
digitalWrite(13, LOW);
}

void loop() {
// read the sensor:
sensorValue = analogRead(sensorPin);

// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);

// fade the LED using the calibrated value:
analogWrite(ledPin, sensorValue);
}
``````
• Fading - Use an analog output (PWM pin) to fade an LED.

• Smoothing - Smooth multiple readings of an analog input.