DIY Arduino Unit Converter: How to Use LCD:
by AJ_Smoothie in Circuits > Arduino
15338 Views, 18 Favorites, 0 Comments
DIY Arduino Unit Converter: How to Use LCD:
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
Ingredients:
- Arduino
- Breadboard
- LCD*
- 2x 50k pots
- 150 ohm resistor
- Jumper wires, and tons of them!
The LCD
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.
Troubleshooting:
- All I see is white squares or the screen is just blue (or your lcd backlight color)!
- The screen does not light up!
- 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 ]
See the revised code. Upload this to your Arduino and see what happens!
Hooking Up the Second Potentiometer
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.
<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.
Printing the Pot Value to the LCD
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.
Math!
<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:
- Display the number of inches with the word " inches" after it.
- Clear the screen
- Display the number of feet with the word " feet" after it.
<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); }
More Programming . . .
<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:
- "Welcome to the unit converter!"
- "Adjust the knob" with the number of inches below it. This will be showing for 5 seconds. Turn your pot!
- Print the amount of inches
- Clear
- Print the amount feet
- 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); }
Adding More Math!
I added a conversion to feet, yards, centimeters, and meters. Don't forget to define what these are in the beginning of the sketch.
IHere's an overview of the sketch above:
- On startup, "Welcome to the unit converter"
- "Adjust the knob" with the amount of inches below. Turn the knob!
- Displays the amount of inches
- Displays the amount of feet
- Displays the amount of centimeters
- Displays the amount or meters
Conclusion:
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