Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple analog input to serial output conversion
Barebones sketch to read an analog voltage between 0 and 100mV.
An internal reference voltage of 1.1v is used to scale the digital conversion.
The maximum output of the HCHO sensor (typically 100mV) is converted to a value of 103.
This value of 103 should equal a reading of 1000ppb HCHO.
If the maximum output of the sensor is greater than 100mV, a reading of greater than 1000ppb is posible.
int analogPin = 3; // Tip of output HCHO sensor jack connected to analog pin 3
// Ring of HCHO sensor connector connected to GND
int adcRaw; // variable to store the sensor value read
float voltRaw;
int ppbHCHO; // variable to store a result
void setup()
analogReference(INTERNAL); // Set analog reference to 1.1 volts
Serial.begin(9600); // setup serial
void loop()
// read the input pin
// adcRaw = analogRead(analogPin);
//Generate Random Value for Testing
adcRaw = random(0,1023);
voltRaw = adcRaw * (1.01 / 1023.0); //this should be the actual output voltage of the sensor
// ppbHCHO = voltRaw*10; // scale the result between 0 and 1023
// Print to serial monitor
//NOTE: "\t" prints a tab character, if you want a TAB Seperated Values File with a timestamp from Coolterm.
//Otherwise use "," for a CSV, which has broader compatibility.
Serial.print(adcRaw); // this is the conversion of the analog voltage to a digital number
Serial.println(voltRaw); // This might approximate ppb
delay(3000); // take a reading and print to the serial monitor every 3 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment