Tag Archives: Arduino

Arduino 11: A Character LCD

A character LCD makes user interaction with your projects a lot easier, but programming one can be a bit difficult. Luckily release 17 of the Arduino software has an excellent library, LiquidCrystal.h, which simplifies matters. The LCD must have a driver compatible with the Hitachi HD44780 for the library to work, so check your spec sheet. Such a display will have a 14-pin interface (16 if it is backlit). It’s nice to have the holes for the interface pins all in a line as in the diagram below, so that you can solder a 14 pin header straight across, and the display will neatly pop into a breadboard.

Pin Sym Function Pin Sym function
1 Vss Ground 9 D2 Data bit
2 Vdd +5V 10 D3 “”
3 VL Contrast 11 D4 “”
4 RS Data input 12 D5 “”
5 R/W Read/write-low 13 D6 “”
6 E Enable 14 D7 “”
7 D0 Data bit 15 backlight
8 D1 “” 16 backlight

Check your spec sheet for the pin numbering – the first hole isn’t always 1! Pins 1 and 2 go to ground and +5V to power the board. If your display has a backlight, pins 15 and 16 can go to ground and +5V as well. Pin 3 should go to the middle pin of a potentiometer, whose ends also go to ground and +5V, to control display contrast. All the other pins are directed to digital output pins on the Arduino. Pins 4, 5, and 6 are control pins, and 7 through 14 are data pins. The Arduino library can send data to the LCD in 4-bit mode, meaning it only needs LCD pins 11-14 (data bits 4,5,6 and 7). Therefore pins 7-10 on the LCD can be left unattached, freeing up Arduino pins for other uses.

Example

Note that in the schematic, pins D2, D3 etc refer to the digital output pins on the Arduino (not the data pins on the LCD as in the table above).  The only part used besides the LCD is a 10K potentiometer on pin 3 of the LCD for contrast control.

The program follows.

#include <LiquidCrystal.h>
int rs = 12;
int rw = 11; // or you could ground lcd pin11
int e = 10;
int d4 = 5;
int d5 = 4;
int d6 = 3;
int d7 = 2;
int row = 16;
int col = 2;
LiquidCrystal lcd(rs, rw, e, d4, d5, d6, d7);
void setup() {
   lcd.begin(col, row);
   lcd.print("hello, world!");
}
void loop() {

   for (int n=0; n<10; n++) {
      lcd.setCursor(0,1); //rows and columns start at 0
      lcd.print(n);
      delay(500);
   }
}

Programming Notes

The first line of your program is a call to the library:

#include <LiquidCrystal.h>

The following functions are in the LiquidCrystal library:

LiquidCrystal(rs, rw, e, d4, d5, d6, d7);

Where the arguments rs, rw, etc are the pin numbers on the Arduino that correspond to the specified pins on the LCD. This command goes right after the include statement, and before the void setup() statement. Note the rw pin must also be grounded.

lcd.begin(col, row);

sets the number of rows and columns on the display.

lcd.print(data);

prints the data to the LCD. Data can be a string in quotes, or a variable.

lcd.clear();

clears the lcd and moves the cursor to the top left corner.

lcd.setCursor(c,r);

moves the cursor to column c, row r (columns run 0-15, rows 0-1).

Arduino 9: The H-Bridge

An H-Bridge is like an electric double-switch, or double-relay. A small current from the Arduino at one of two pins can turn on a much larger current at two other pins. This is useful for controlling two-state devices, like motors that you want to run both forwards and backwards. For this project I used an SN75441 H-bridge chip.

Example:

Have the Arduino read the state of a switch to control a high-voltage motor running forwards and backwards. Use a 9V battery to power the Arduino (remember to change the jumper pin), so Vin is 9V. Connect the two motor wires directly to the H-bridge chip pins labeled “Motor”, and connect both pins 4 and 5 of the H-bridge to ground.

Schematic:

D2, D3 and D4 are digital pins on the Arduino

Program:

int motorPin0 = 2;

int motorPin1 = 3;

int switchPin = 4;

int state = 0;

void setup() {

pinMode(motorPin0, OUPUT);

pinMode(motorPin1, OUTPUT);

pinMode(switchPin, INPUT);

}

void loop() {

state = digitalRead(switchPin);

switch(state) {

case 0:

digitalWrite(motorPin0, HIGH);

digitalWrite(motorPin1, LOW);

break;

case 1:

digitalWrite(motorPin0, LOW);

digitalWrite(motorPin1, HIGH);

break;

}

}

Note the use of the switch statement:

switch(variable){

case value1:

(code block here);

break;

case value2:

(code block here);

break;

case value3:

(code block here);

break;

default:

(code block here);

break;

}

Where variable can take on the values value1, value2, value3, etc. (which are usually integer values), and based on the value of variable, different code is run. The default option is run if the value of variable doesn’t match any of the other cases. The keyword break tells the program to jump to the end of the switch statement, skipping the code in between.

Arduino 8: Relays

A relay is a switch that allows a small current from the Arduino to turn on or off a device drawing a much larger current.  This is usually a concern if you want the Arduino to control an electric motor, which will typically want to draw more current than the Arduino can handle. Relays are the key to turning your Arduino into a controller for any motorized project, and are great fun in combination with any motorized construction kit like Lego, K’Nex or FischerTechnik. I bought a set of Fischertechnik Mechanic and Static kits with a grant a few years back, and combined with the Arduinos they make wonderful platforms for computer-controlled mechanical models, like working elevators and light-seeking robots.

Relays come in different shapes, but typically have four pins:   an input voltage Vin from a power supply, ground, a data pin to turn the relay on or off (this is what the Arduino does), and an output voltage pin to the device you want to control.  Buy a relay that can be controlled by 5V DC (the Arduino) – the output depends on what you want to control with it. Lots of larger motors want 12V DC or so, so get a relay that can handle a bit more than that. The relay should come with a spec sheet telling you which pin is which. If not, Google “relay spec sheet” and the part number, and you’ll probably find the document online.

Image made with Fritzing (http://fritzing.org/)

In this project we have the Arduino read the state of a pushbutton, and use that to turn a small DC motor on and off. You might wonder what’s the point – just wire the pushbutton to the motor power supply and forget the Arduino. The point is, the pushbutton can be replaced by any sensor, so the Arduino can use any input signal to trigger the motor.  Use a 9V battery to power the Arduino and motor, so it doesn’t have to run off the weaker supply delivered by the USB cable. This means you need to get a 9V battery clip (or wall power supply), and solder it to a 2.1mm center-positive plug to fit the Arduino power connector. The new Duemilanove board will automatically sense the power supply when connected, but older boards have a little jumper next to the USB connector that needs to be switched from USB to EXT.

In the picture, the top left pin of the relay is the data pin, going to pin 13; the bottom right pin is ground; the pin just right of that is Vin on the Arduino; and the bottom right pin goes to the motor’s + terminal. The other motor terminal goes to ground. The pushbutton has one terminal connected to +5V, the other connected both to digital input pin 2 and to a 10K resistor leading to ground.

Program:

int buttonPin = 2;
int relayPin = 13;
int state=0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
state = digitalRead(buttonPin);
if (state==HIGH) {
digitalWrite(relayPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
}
delay(100);
}