Académique Documents
Professionnel Documents
Culture Documents
GND to Pin 3 and Pin 8 each connected with 220 ohm resistors.
Code
Seven segment displays are of two types: common anode and common cathode. The Internal
structure of both types is nearly the same. The difference is the polarity of the LEDs and
common terminal. In a common cathode seven-segment display (the one we used in the
experiments), all seven LEDs plus a dot LED have the cathodes connected to pins 3 and pin 8.
To use this display, we need to connect GROUND to pin 3 and pin 8 and, and connect +5V to
the other pins to make the individual segments light up. The following diagram shows the
internal structure of common-cathode seven-segment display:
segment display.
Common anode
The seven segment are labelled a-g, with the dot being "dp," as shown in the figure below:
SSD Configuration
To display a particular number, you turn on the individual segments as shown in the table below:
8 07F 07F on on on on on on on
Experiment 1
In this experiment, we will simply turn on and turn off the LEDs to get familiar with how a
seven-segment display works.
Hardware Required
1 x seven segment display (common cathode)
1 x Arduino MEGA 2560
1 x breadboard
jumper wires
Wiring Diagram
In this circuit, the pins of seven-segment display are connected to Arduino pins 2-9, as shown in
the table below. Common pins (pin 3 and pin 8) are connected to GND and dp is left
unconnected, because it is not used in this experiment
1(e) 6 orange
2(d) 5 white
c 4 yellow
5(dp) -
6(b) 3 red
7(a) 2 blue
9(f) 7 cyan
10(g) 8 green
Code
void setup()
{
// define pin modes
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
void loop()
{
// loop to turn leds od seven seg ON
for(int i=2;i<9;i++)
{
digitalWrite(i,HIGH);
delay(600);
}
delay(1000);
Experiment 2
Description
In this tutorial, we will be interfacing a seven segment display with Arduino mega and learn to
display a count down from nine with a delay of a second, on seven segment display.
Hardware Required
The hardware required for this experiment is the same as for Experiment 1.
Wiring Diagram
The wiring diagram for this experiment is the same as the circuit for Experiment 1.
Code
// make an array to save Sev Seg pin configuration of
numbers
//function header
void Num_Write(int);
void setup()
{
// set pin modes
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
void loop()
{
//counter loop
Overview
by Simon Monk
In this lesson, you will learn how to wire up and use an alphanumeric LCD display.
The display has an LED backlight and can display two rows with up to 16 characters on each
row. You can see the rectangles for each character on the display and the pixels that make up
each character. The display is just white on blue and is intended for showing text.
In this lesson, we will run the Arduino example program for the LCD library, but in the next
lesson, we will get our display to show the temperature and light level, using sensors.
There are quite a few connections to be made. Lining up the display with the top of the
breadboard helps to identify its pins without too much counting, especially if the breadboard has
its rows numbered with row 1 as the top row of the board. Do not forget, the long yellow lead
that links the slider of the pot to pin 3 of the display. The 'pot' is used to control the contrast of
the display.
You may find that your display is supplied without header pins attached to it. If so, follow the
instructions in the next section.
Arduino Code
by Simon Monk
The Arduino IDE includes an example of using the LCD library which we will use. You can find
this on the File menu under Examples Liquid Crystal HelloWorld.
This example uses different pins to the ones we use, so find the line of code below:
Copy Code
Copy Code
Upload the code to your Arduino board and you should see the message 'hello, world' displayed,
followed by a number that counts up from zero.
Copy Code
1. #include <LiquidCrystal.h>
This tells Arduino that we wish to use the Liquid Crystal library.
Next we have the line that we had to modify. This defines which pins of the Arduino are to be
connected to which pins of the display.
Copy Code
Display Pin Name Display Pin Number Arduino Pin (in this example) RS 4 7
E68 D4 11 9 D5 12 10 D6 13 11 D7 14 12
After uploading this code, make sure the backlight is lit up, and adjust the
potentiometer all the way around until you see the text message
Copy Code
1. lcd.begin(16, 2);
2. lcd.print("hello, world!");
The first tells the Liquid Crystal library how many columns and rows the display has. The second
line displays the message that we see on the first line of the screen.
In the 'loop' function, we aso have two commands:
Copy Code
1. lcd.setCursor(0, 1);
2. lcd.print(millis()/1000);
The first sets the cursor position (where the next text will appear) to column 0 & row 1. Both
column and row numbers start at 0 rather than 1.
The second line displays the number of milliseconds since the Arduino was reset.
Overview
by Simon Monk
In this lesson, you will build on what we have learnt in lesson 11 and use a LCD display to show
the temperature and light intensity.
Light intensity is measured using the same photocell that you used in lesson 9.
To measure the temperature, you will use a temperature measurement chip. This device has just
three leads two for 5V and GND and the third lead is connected directly to an analog input on the
Arduino.
Breadboard Layout
by Simon Monk
The breadboard layout is based on the layout from lesson 11, so if you still have this on the
breadboard it will simplify things greatly.
There are a few jumper wires that have been moved slightly on this layout. In particular, those
near the pot.
The photocell, 1 k resistor and TMP36 are all new additions to the board. The TMP36 has its
curved face towards the display.
Arduino Code
by Simon Monk
The sketch for this is based on that of lesson 11. Load it up onto your Arduino and you should
find that warming the temperature sensor by putting your finger on it will increase the
temperature reading.
Also if you wave your hand over the photocell blocking out some of the light, the reading will
decrease.
Copy Code
1. /*
2. Adafruit Arduino - Lesson 12. Light and Temperature
3. */
4.
5. #include <LiquidCrystal.h>
6.
7. int tempPin = 0;
8. int lightPin = 1;
9.
10. // BS E D4 D5 D6 D7
11. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
12.
13. void setup()
14. {
15. lcd.begin(16, 2);
16. }
17.
18. void loop()
19. {
20. // Display Temperature in C
21. int tempReading = analogRead(tempPin);
22. float tempVolts = tempReading * 5.0 / 1024.0;
23. float tempC = (tempVolts - 0.5) * 100.0;
24. float tempF = tempC * 9.0 / 5.0 + 32.0;
25. // ----------------
26. lcd.print("Temp F ");
27. lcd.setCursor(6, 0);
28. lcd.print(tempF);
29.
30. // Display Light on second row
31. int lightReading = analogRead(lightPin);
32. lcd.setCursor(0, 1);
33. // ----------------
34. lcd.print("Light ");
35. lcd.setCursor(6, 1);
36. lcd.print(lightReading);
37. delay(500);
38. }
Copy Code
1. // BS E D4 D5 D6 D7
2. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
This makes things easier if you decide to change which pins you use.
In the 'loop' function there are now two interesting things going on. Firstly we have to convert
the analog from the temperature sensor into an actual temperature, and secondly we have to work
out how to display them.
Copy Code
The raw reading from the temperature sensor is first multiplied by 5 and then divided by 1024 to
give us the voltage (between 0 and 5) at the 'tempPin' analog input.
To convert the voltage coming from the TMP36 into a temperature in degrees C, you have to
subtract 0.5V from the measurement and then multiply by 100.
To convert this into a temperature in Fahrenheit, you then have to multiply it by 9/5 and then add
32.
Displaying changing readings on an LCD display can be tricky. The main problem is that the
reading may not always be the same number of digits. So, if the temperature changed from
101.50 to 99.00 then the extra digit from the old reading is in danger of being left on the display.
To avoid this, write the whole line of the LCD each time around the loop.
Copy Code
1. // ----------------
2. lcd.print("Temp F ");
3. lcd.setCursor(6, 0);
4. lcd.print(tempF);
The rather strange comment serves to remind you of the 16 columns of the display. You can then
print a string of that length with spaces where the actual reading will go.
To fill in the blanks, set the cursor position for where the reading should appear and then print it.
Exactly the same approach is used for displaying the light level. There are no units for the light
level, we just display the raw reading from the analog read.
/*
LiquidCrystal Library - scrollDisplayLeft() and scrollDisplayRight()
This sketch prints "Hello World!" to the LCD and uses the
scrollDisplayLeft() and scrollDisplayRight() methods to scroll
the text.
The circuit:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 8
* LCD D4 pin to digital pin 9
* LCD D5 pin to digital pin 10
* LCD D6 pin to digital pin 11
* LCD D7 pin to digital pin 12
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
http://www.arduino.cc/en/Tutorial/LiquidCrystalScroll
*/
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
delay(1000);
}
void loop() {
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}
#include <LiquidCrystal.h>
#include <string.h>
char message[] = "This is some long message that will end up scrolling";
int previous = 0;
int pos = 0;
void setup() {
lcd.begin(16, 2);
//lcd.print(message);
//if it is then update the display , it must also not equal the previously
previous = (millis()/1000);//Store the current time we entered for comparison on the next cycle
lcd.setCursor(0, 1);//Set our draw position , set second param to 0 to use the top line
char lcdTop[16];//Create a char array to store the text for the line
int copySize = 16; // What is the size of our screen , this could probably be moved outside the
loop but its more dynamic like this
//if the message is bigger than the current buffer use its length instead;
copySize = strlen(message);
}
//Store the current position temporarily and invert its sign if its negative since we are going in
reverse
if(tempPos < 0)
tempPos = -(tempPos);
//Build the lcd text by copying the required text out of our template message variable
memcpy(&lcdTop[0],&message[tempPos],copySize);
//Increase the current position and check if the position + 16 (screen size) would be larger than
the message length , if it is go in reverse by inverting the sign.
pos += 1;
pos = -(pos);
void loop() {
printLine(1);