Accurately reading analog pins, making Half-Byte Tiny Basic do the same and a fix for LiquidCrystal_I2C

temperaturesensor-128-300-225-80While experimenting with a new to me temperature sensor, I was a bit puzzled as to how to read the sensor. It has three pins, voltage, ground and signal. Well, it is obvious on how to connect the pins, power and ground and, since it was temperature sensor spitting out an analog signal, I figured one of the analog pins would suffice. I was correct about that, but, then, I did not know how to interpret the results. There is actually very little on the ‘net about this thing. It is a TMP-01 from OSEPP. Even the OSEPP web page has little on the device. I actually found out how to interpret the device on the SparkFun Galileo page (https://learn.sparkfun.com/…/sik-galileo—part-7-reading-a…)

One thing about Arduino chips (like the 328) have a built in voltage measuring mechanism. Yes, that’s right, it has a built in voltage meter of sorts.

On that page, there is this code:

Example sketch 07

TEMPERATURE SENSOR

Use the “serial monitor” window to read a temperature sensor.

The TMP36 is an easy-to-use temperature sensor that outputs
a voltage that’s proportional to the ambient temperature.
You can use it for all kinds of automation tasks where you’d
like to know or control the temperature of something.

More information on the sensor is available in the datasheet:
http://cdn.sparkfun.com/datasheets/Sensors/…/TMP35_36_37.pdf

Even more exciting, we’ll start using the Arduino’s serial port
to send data back to your main computer! Up until now, we’ve
been limited to using simple LEDs for output. We’ll see that
the Arduino can also easily output all kinds of text and data.

Hardware connections:

Be careful when installing the temperature sensor, as it is
almost identical to the transistors! The one you want has
a triangle logo and “TMP” in very tiny letters. The
ones you DON’T want will have “222” on them.

When looking at the flat side of the temperature sensor
with the pins down, from left to right the pins are:
5V, SIGNAL, and GND.

Connect the 5V pin to 5 Volts (5V).
Connect the SIGNAL pin to ANALOG pin 0.
Connect the GND pin to ground (GND).

/*

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/

// We’ll use analog input 0 to measure the temperature sensor’s
// signal pin.

const int temperaturePin = 0;

void setup()
{
// In this sketch, we’ll use the Arduino’s serial port
// to send text back to the main computer. For both sides to
// communicate properly, they need to be set to the same speed.
// We use the Serial.begin() function to initialize the port
// and set the communications speed.

// The speed is measured in bits per second, also known as
// “baud rate”. 9600 is a very commonly used baud rate,
// and will transfer about 10 characters per second.

Serial.begin(9600);
}

void loop()
{
// Up to now we’ve only used integer (“int”) values in our
// sketches. Integers are always whole numbers (0, 1, 23, etc.).
// In this sketch, we’ll use floating-point values (“float”).
// Floats can be fractional numbers such as 1.42, 2523.43121, etc.

// We’ll declare three floating-point variables
// (We can declare multiple variables of the same type on one line:)

float voltage, degreesC, degreesF;

// First we’ll measure the voltage at the analog pin. Normally
// we’d use analogRead(), which returns a number from 0 to 1023.
// Here we’ve written a function (further down) called
// getVoltage() that returns the true voltage (0 to 5 Volts)
// present on an analog input pin.

voltage = getVoltage(temperaturePin);

// Now we’ll convert the voltage to degrees Celsius.
// This formula comes from the temperature sensor datasheet:

degreesC = (voltage – 0.5) * 100.0;

// While we’re at it, let’s convert degrees Celsius to Fahrenheit.
// This is the classic C to F conversion formula:

degreesF = degreesC * (9.0/5.0) + 32.0;

// Now we’ll use the serial port to print these values
// to the serial monitor!

// To open the serial monitor window, upload your code,
// then click the “magnifying glass” button at the right edge
// of the Arduino IDE toolbar. The serial monitor window
// will open.

// (NOTE: remember we said that the communication speed
// must be the same on both sides. Ensure that the baud rate
// control at the bottom of the window is set to 9600. If it
// isn’t, change it to 9600.)

// Also note that every time you upload a new sketch to the
// Arduino, the serial monitor window will close. It does this
// because the serial port is also used to upload code!
// When the upload is complete, you can re-open the serial
// monitor window.

// To send data from the Arduino to the serial monitor window,
// we use the Serial.print() function. You can print variables
// or text (within quotes).

Serial.print(“voltage: “);
Serial.print(voltage);
Serial.print(” deg C: “);
Serial.print(degreesC);
Serial.print(” deg F: “);
Serial.println(degreesF);

// These statements will print lines of data like this:
// “voltage: 0.73 deg C: 22.75 deg F: 72.96”

// Note that all of the above statements are “print”, except
// for the last one, which is “println”. “Print” will output
// text to the SAME LINE, similar to building a sentence
// out of words. “Println” will insert a “carriage return”
// character at the end of whatever it prints, moving down
// to the NEXT line.

delay(1000); // repeat once per second (change as you wish!)
}

float getVoltage(int pin)
{
// This function has one input parameter, the analog pin number
// to read. You might notice that this function does not have
// “void” in front of it; this is because it returns a floating-
// point value, which is the true voltage on that pin (0 to 5V).

// You can write your own functions that take in parameters
// and return values. Here’s how:

// To take in parameters, put their type and name in the
// parenthesis after the function name (see above). You can
// have multiple parameters, separated with commas.

// To return a value, put the type BEFORE the function name
// (see “float”, above), and use a return() statement in your code
// to actually return the value (see below).

// If you don’t need to get any parameters, you can just put
// “()” after the function name.

// If you don’t need to return a value, just write “void” before
// the function name.

// Here’s the return statement for this function. We’re doing
// all the math we need to do within this statement:

return (analogRead(pin) * 0.004882814);

// This equation converts the 0 to 1023 value that analogRead()
// returns, into a 0.0 to 5.0 value that is the true voltage
// being read at that pin.
}

BUT

There is more to it. In order to get a more accurate voltage, you have come up with the reference voltage and use it instead of the .004882814 used above.

 This site: https://hackingmajenkoblog.wordpress.com/…/making-accurate…/
 has a really nice explanation and some code to get the reference voltage:
 long readVcc() {
 long result;
 // Read 1.1V reference against AVcc
 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
 delay(2); // Wait for Vref to settle
 ADCSRA |= _BV(ADSC); // Convert
 while (bit_is_set(ADCSRA,ADSC));
 result = ADCL;
 result |= ADCH<<8;
 result = 1125300L / result; // Back-calculate AVcc in mV
 return result;
 }
And call it like this:
 unsigned int ADCValue;
 double Voltage;
 double Vcc;

 Vcc = readVcc()/1000.0;
 ADCValue = analogRead(0);
 Voltage = (ADCValue / 1024.0) * Vcc;

Call that function to get your reference and use that where ever you need, such as the temperature sensor.

Making Half-Byte Tiny Basic Accurately Those Analog Pins too!

I am adding a function to Half-Byte Tiny Basic to return this value.
It goes like this:
250 a=VOLT(0)
260 print a,”,”,a/1000
Line 250 gets the reference voltage
Line 260 displays the voltage and the voltage divided by 1000. The returned voltage is in the thousands, so you must divide by 1000 to get the actual value.

Using AREAD in conjunction with VOLT should give you the ability to read, accurately, the analog pins that have some kind of sensor attached to them that does not need a library to read them, like the TEMP-01.

Fix for LiquidCrystal_I2C not displaying the whole string

This also brings me to another issue…the LiquidCrystal_I2C library.  The newer versions of the GCC compiler that is used in the latest versions of the Arduino compiler have a problem: they only display one character instead of the entire string.  I spent a lot of time trying to figure out what I was doing wrong, but, it turns out, there is a bug in the library.  I found another that corrects the bug and you can download it from here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s