loading
Picture of DIY Arduino Unit Converter: How to use LCD:
flowchart1.png
Here's a horrible video of it working. I'll upload a better one later.

 
Of course this is for the make to learn youth contest, so here are the answers to all your questions!

What did I make?
Well I thought it was time my Arduino learned to do some math, so I began these project with it printing out all the results to the serial monitor. After seeing my LCD screen sitting forlornly in the corner of my electronics box, I then decided it was time to make a project!
How did I make it?
Well I made use a small breadboard, some potentiometers, jumper wires, my lcd screen, and my Arduino. Programming this was the hardest part, and it was very educational for me. My ideas changed? Yes, actually, quite drastically. I was never intending to use my LCD screen until I happend to notice it, and decided it was time to learn how to use it. I'm 15 years old made this project by myself. A fellow instructables member laxap helped me with code in step 6
Where did I make it?
Haha, where? Like, everywhere in my house, but mostly on my kitchen table. Since our iMac was in the shop (no, it wasn't broken, we were just upgrading it) I was using my Dad's laptop to type all the code. I did this as a school project for my electronics, and hopefully, I'll earn a good grade :D
What did I learn?
I learned a whole lot, like using my LCD screen, using numbers with Arduino, making it do math and all that kind of stuff. If I could do it again differently, I not exactly sure at the moment what I would change. Oh, and I'm proudest of my incredible (assisted :D) math :D
 
Remove these adsRemove these ads by Signing Up

Step 1: Ingredients:

Electronics
  • Arduino
  • Breadboard
  • LCD*
  • 2x 50k pots
  • 150 ohm resistor
  • Jumper wires, and tons of them!
If you do not want to use a LCD then you will have to use serial monitor. Wherever you see lcd.print just change it to Serial.print. Instead of lcd.begin change it to Serial.begin(9600);.

Step 2: The LCD

Picture of The LCD
Screen Shot 2013-03-04 at 8.44.27 AM copy.png
Screen Shot 2013-03-04 at 8.44.27 AM.png
So the first thing I did was go to the Arduino website and see how to hook up my LCD screen. I've attached the schematic.
For the backlight hook up pin (K) on the LCD to ground, and pin (A) on the LCD to pin 10 on Arduino through the 150 ohm resistor.

We need to learn to how to use this LCD screen, so I've included a little information on this. Arduino already has a library for the LCD, so we need to include this. We can accomplish this by typing in our code before the setup, #include <LiquidCrystal.h> . Now our LCD library is included. The next thing to do is to tell Arduino what pins we are hooking up to the LCD. We do this by typing in this before the setup function, LiquidCrystal lcd(12, 11, 5, 4, 3, 2); These are the pins Arduino will send data to the LCD through.
So this is what our sketch is going to look like so far.


Now lets learn a little bit about using the LCD functions.
  • lcd.begin(16,2); // this is for setting up the number of column and rows. If you look in pictures 2 and 3 you can see the amount of columns and in picture 3 you can see the amount of rows.
  • lcd.print(" ");   // this is how we print text on the screen. Your text goes in between the the quotes. The quotes are needed.
  • lcd.clear();      // this is how we clear all text from the screen.
  • lcd.setCursor(0,1); // This is how we put text on the second row.
So put all this to a little bit of use. Hook up your screen, then type in this code.
If you copy this, paste in your sketch, and upload to your board, you should see the LCD light up and the text appear, "I'm cool".
 
Troubleshooting:
  • All I see is white squares or the screen is just blue (or your lcd backlight color)!
  • The screen does not light up!
Solution:
  • Turn the potentiometer all the way to one side then the other. You should see the text appear. If so, slowly adjust the pot until the text is clear
  • Make sure all your connections are right.
  • Make sure [ lcdbl ] is set to [ OUTPUT ]
Now, lets put some text on the bottom row. If you recall, we need to offset the cursor. We do this by typing in lcd.setCursor(0,1); 
See the revised code.
Upload this to your Arduino and see what happens!

Step 3: Hooking up the Second Potentiometer

Picture of Hooking up the Second Potentiometer
Alright, now that we know how to use the LCD, we need to move one. The first step is to hook up our second potentiometer. We will do this by hooking up the two outer pins to 5v and GND, and the middle pin the Arduino pin A0.
Now of course we need to set this up with Arduino. This is how:

<pre>int sensorValue = analogRead(A0);  // read the input on analog pin 0:<br />
float inches = sensorValue * (500 / 1023.0);  // Convert the analog reading (which goes from 0 - 1023) to a value (0-500);

Lets examine this line in more detail

  • int sensorValue = analogRead(A0) -- we are setting the words 'sensorValue' equal to the analog reading from pin A0.
  • float inches = sensorValue * (500/ 1023.0); -- we are setting the word 'inches' to equal our new reading, (0-500); You may change the words 'inches' to 'feet', if you want the pot to adjust the amount of feet.
Basically all this does is tell Arduino that we have a analog reading (which goes from 0-1023) at pin A0. The next line is for converting the reading (0-1023) to whatever we like, which in this case, is 500. We will need to put this in the loop block. Here's what our code is looking like so far.

<pre>#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10;  // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10

void setup()
{
  lcd.begin(16,2);
  digitalWrite(lcdbl, HIGH);
  pinMode(lcdbl, OUTPUT); // set pin 11 to output
}
void loop()
{
  int sensorValue = analogRead(A0);  // read the input on analog pin 0:
  float inches = sensorValue * (500 / 1023);  
} 

Notice that I have gotten rid of our test text. I don't really think we'll be needing it :D.

Step 4: Printing the Pot Value to the LCD

Picture of Printing the Pot Value to the LCD
So we need to tell the lcd to print something, but what do we tell it to print? What we do is type in
lcd.print(inches); Now why inches? Well, remember, we named the analog read from A0 'inches' So this is what is what we should put in. Don't use quotes, because quotes is just for putting down text.
<pre>#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10;  // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10

void setup()
{
  lcd.begin(16,2);
  digitalWrite(lcdbl, HIGH);
  pinMode(lcdbl, OUTPUT); // set pin 11 to output
}
void loop()
{
  int sensorValue = analogRead(A0);  // read the input on analog pin 0:
  float inches = sensorValue * (500 / 1023.0); 
  
  
 lcd.print(inches);
 

} 
So we should have our reading printed to our LCD right!? So upload the code! But oh no! It's just a big jumble of numbers filling the whole screen! Why is this happening!? Well studying our code, we see that this function is in the loop section, so any operations inside of it is going to repeat on and on. If you noticed, there is no delay or breaks, so it's just going to print like crazy to our LCD. This is how we are going to fix our dilemma. We are going to use a simple lcd.clear function to clear the screen, then add a small delay. It will now print the number of inches, wait a fraction of a second, clear the screen, then repeat this over and over until next reset. So our new code will look like this.
<pre>#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10;  // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10

void setup()
{
  lcd.begin(16,2);
  digitalWrite(lcdbl, HIGH);
  pinMode(lcdbl, OUTPUT); // set pin 11 to output
}
void loop()
{
  int sensorValue = analogRead(A0);  // read the input on analog pin 0:
  float inches = sensorValue * (500 / 1023.0);  
  
 lcd.print(inches);
 delay(100);
 lcd.clear();
 

} 

So now what it's going to do is to print the amount analogRead value (inches), leave it on the LCD for 100 milliseconds, clear it, and start all over. Now you should be able to turn your potentiometer and see a live update of what's happening. Now turn the knob to like a big number, say 350.

Step 5: Math!

Picture of Math!
Now it's time to do a little math. I want my Arduino to tell me how many inches there are, then, have the screen go blank, then, tell me how many feet there is. In order to find out how many feet there are in inches we need to divide by 12. So, this is how we are going to set up our line of code.
<pre>feet = (inches / 12);

If we wanted to convert inches to centimeters we would do:

<pre>
centimeters = (inches * 2.54);

So our code could look something like this. Notice I have inserted into the beginning of the sketch, int feet; If you do not do this, you wil get an error message as shown in the picture. You must define what "feet" is. The code below should do this:

  1. Display the number of inches with the word " inches" after it.
  2. Clear the screen
  3. Display the number of feet with the word " feet" after it.
If you left the inches to 350 like I did in step four the amount fo feet should be 29.
<pre>#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10;  // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10
int feet;
int wait = 3000;    // assign the word "wait" to 3000 milliseconds


void setup()
{
  lcd.begin(16,2);
  digitalWrite(lcdbl, HIGH);
  pinMode(lcdbl, OUTPUT); // set pin 11 to output
 
}
void loop()
{
  
   
 int sensorValue = analogRead(A0);  // read the input on analog pin 0:
 float inches = sensorValue * (500 / 1023.0);  
  
  delay(1000); 
  lcd.print(inches);
  lcd.print(" inches:");
  delay(wait);
  lcd.clear();
  delay(1000);  
  
 feet = (inches / 12); // conversion from feet to inches Here this is telling Arduino that
                       // feet is equal to inches divided by 12
 lcd.print(feet);
 lcd.print(" feet:");
 delay(wait);
 lcd.clear();
 delay(1000);
} 





Step 6: More Programming . . .

The problem with the sketch (above) we have so far is that we cannot change our inches with a live update. The code above will only print the amount of inches once. Now this is fine for the amount of feet, or whatever we are converting to, but for our variable, this is horrible! We can't even choose what number we want for inches! In order to fix this, I used a small procedure to print the data (inches) to the LCD and clear it 50 times, which gives me a pretty good number change according to my turning of the pot. The code below repeats all the code in between the blocks 50 times, and with the delay set at 100, that about 5 seconds (50*100).
<pre> for(int i=0; i<50; ++i) 
   {
    int sensorValue = analogRead(A0);  // read the input on analog pin 0:
    float inches = sensorValue * (500 / 1023.0);  
  
    lcd.print("Adjust the knob");
    lcd.setCursor(0,1);
    lcd.print(inches);
    delay(100);
    lcd.clear();
  }

In the sketch above, instead only seeing the amount of inches once, it will repeat this many times, allowing you to see it as it updates (in unison with the turning of the second pot).

Uploading the code below should do this:

  1. "Welcome to the unit converter!"
  2. "Adjust the knob" with the number of inches below it. This will be showing for 5 seconds. Turn your pot!
  3. Print the amount of inches
  4. Clear
  5. Print the amount feet
  6. Back to "Adjust the knob"
<pre>#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int lcdbl = 10;  // lcdbl=LCD Backlight: hooking up the lcdbacklight to pin 10
int feet;
int wait = 3000;    // assign the word "wait" to 3000 milliseconds


void setup()
{
  lcd.begin(16,2);
  digitalWrite(lcdbl, HIGH);
  pinMode(lcdbl, OUTPUT); // set pin 11 to output
  
  lcd.print("Welcome to the");
  lcd.setCursor(0,1);          // the cursor is like the rows on your LCD. After this, it will print
                               // the text on the bottom line of your LCD screen.
  lcd.print("unit converter!");
  delay(wait);                 /* if you look here, intead of the usual time in milliseconds
                               it says "wait". If we look what "wait" is, at the beginning of the code,
                               we see that "wait" is assigned to 3000 milliseconds, so whenever I type in
                               delay(wait); it wil have a delay of 3000 milliseconds, or 3 seconds. */
                          
  lcd.clear();
  delay(1000);
}
void loop()
{
  
  for(int i=0; i<50; ++i) 
   {
    int sensorValue = analogRead(A0);  // read the input on analog pin 0:
    float inches = sensorValue * (500 / 1023.0);  
  
    lcd.print("Adjust the knob");
    lcd.setCursor(0,1);
    lcd.print(inches);
    delay(100);
    lcd.clear();
   }
   
 int sensorValue = analogRead(A0);  // read the input on analog pin 0:
 float inches = sensorValue * (500 / 1023.0);  
  
  delay(1000); 
  lcd.print(inches);
  lcd.print(" inches:");
  delay(wait);
  lcd.clear();
  delay(1000);  
  
 feet = (inches / 12); // conversion from feet to inches Here this is telling Arduino that
                        // feet is equal to inches divided by 12
 lcd.print(feet);
 lcd.print(" feet:");
 delay(wait);
 lcd.clear();
 delay(1000);
} 



Step 7: Adding More Math!

Here is the final code in which Arduino converts the amount of inches to feet, yards, centimeters, and meters. Then it returns to the "Adjust knob" Go ahead and work through the whole code, and make sure you understand everything.
I added a conversion to feet, yards, centimeters, and meters. Don't forget to define what these are in the beginning of the sketch.
I
Here's an overview of the sketch above:
  1. On startup, "Welcome to the unit converter"
  2. "Adjust the knob" with the amount of inches below. Turn the knob!
  3. Displays the amount of inches
  4. Displays the amount of feet
  5. Displays the amount of centimeters
  6. Displays the amount or meters

Step 8: Conclusion:

Well I hope you guys enjoyed this Ible, and most of all, learned a lot about using the LCD screen, and making Arduino do some math. For more information on math and Arduino visit http://www.ladyada.net/learn/arduino/lesson4.html
If you learned a lot, and liked this lble, vote is as a winner for the Make-to-learn Youth Contest, and also see my other lbles!

If I made any mistakes please tell me about them!

~electricloser