Tag Archives: Display

Seven segment display interface with Arduino (Without library)

A seven-segment display is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix displays. In this tutorial you will learn about seven segment display interface with Arduino, its possibilities & Arduino code. Seven-segment displays may use a liquid crystal display (LCD), a light-emitting diode (LED) for each segment, an electrochromic display, or other light-generating or controlling techniques such as cold cathode gas discharge (Panaplex), vacuum fluorescent (VFD), incandescent filaments (Numitron), and others.

In a simple LED package, typically all of the cathodes (negative terminals) or all of the anodes (positive terminals) of the segment LEDs are connected and brought out to a common pin; this is referred to as a “common cathode” or “common anode” device. Hence a 7 segment plus decimal point package will only require nine pins, though commercial products typically contain more pins, and/or spaces where pins would go, in order to match standard IC sockets.

Characters on seven segment display

The segments of a 7-segment display are referred to by the letters A to G, where the optional decimal point (an “eighth segment”, referred to as DP) is used for the display of non-integer numbers.

You can display 0-9 decimals on seven segment display. The most common patterns used for each of these is:

To display hexadecimals, you have to turn on segments to represent A, B, C & D. Same can be displayed as;

So, using seven segment display you can easily display hexadecimal numbers from 0 to F. To represent same, you can refer following table:

Digit Display gfedcba abcdefg a b c d e f g
0 0 0x3F 0x7E on on on on on on  
1 1 0x06 0x30   on on        
2 2 0x5B 0x6D on on   on on   on
3 3 0x4F 0x79 on on on on     on
4 4 0x66 0x33   on on     on on
5 5 0x6D 0x5B on   on on   on on
6 6 0x7D 0x5F on   on on on on on
7 7 0x07 0x70 on on on        
8 8 0x7F 0x7F on on on on on on on
9 9 0x6F 0x7B on on on on   on on
A A 0x77 0x77 on on on   on on on
b b 0x7C 0x1F     on on on on on
C C 0x39 0x4E on     on on on  
d d 0x5E 0x3D   on on on on   on
E E 0x79 0x4F on     on on on on
F F 0x71 0x47 on       on on on

Apart from hexadecimals, there are few characters which are being used to display on seven segment display which are as below;

Case A B C D E F G H I J K L M N O P Q R S T U V W X Y
Upper A   C   E F G H I J   L     O P     S   U        
Lower   b c d       h i         n o   q r   t u       y

Few short messages giving some status information (e.g. “no dISC” on a CD player) are also usually represented on seven segment displays. In the case of such messages it is not necessary for every letter to be unambiguous, merely for the words as a whole to be readable. For example:

There are few decoder ICs available in market (like CD4511) which has BCD as input (4 pins) & seven segment pins as output. First we will see how to connect a seven segment display with Arduino without any external IC.

Seven segment display pinout

7 segment pinout

As you can see, all segment LEDs are connected with common pin & separate pinout pins for each LED. Above diagram is common cathode seven segment display, if it is common anode then LED polarity will be reversed compared to common cathode.

Seven segment display interface with Arduino

Seven segment display interface with Arduino

In above connection diagram, common cathode type seven segment is used to interface with Arduino. If you have common anode type seven segment display, you have to connect 

Usually, the display may run without any current limiting resistors. So it is advised to use for protection against high current & display segments burnout. Usually for a commonly used red color seven segment display, each LED segment can draw around 15 mA to illuminated properly, so for 5 volt digital logic circuit, the value of the current limiting resistor would be about 200Ω (5v – 2v) / 15mA, or 220Ω to the nearest preferred value. SO resistor value may vary as per seven segment display technical specifications. You can test each segment LED of your seven segment by applying +1.5V to 5V on each pin and common pin to ground.

Arduino code for seven segment display test without library

You can download above code from here.

Above code is for basic understanding seven segment display’s working operation by Arduino. Basically, it is just particular segment pin HIGH & LOW, but it may be more complex if you have more than one displays (that is different topic of seven segment display multiplexing and same will be posted on #beatyourbit in future). The code is to display 0-9 counts at interval of 1 second. The output will look like,

Thus, It may be little bit long but also without any library you can simply use seven segment display. Similar code you can write by directly writing on GPIO ports (Pure AVR method). Find below code with similar output as above:

You can download above code from here.

Understanding above code:

ATmega328p has total 3 No. of GPIO ports namely Port B, Port C & Port D. Port C is an analog port as it has total six pins which are usually used as A0 to A6 in Arduino. Port B & Port D are digital ports & each port has 7 pins hence total 14 digital pins (D0 to D13). here in above code we have used port addressing, the basic method of AVR programming.

I hope you will learn to use single seven segment display by this tutorial & it will be helpful to display some counts or characters in your DIY stuff. In future you can use external seven segment decoders ICs to run such seven segment displays using minimum pins and also multiplexing of multiple seven segment displays; such tutorials will be uploaded soon on #BeatYourBit website. If you like this tutorial, share it with your friends & any Arduino newbie you know. Give your feedback about this tutorial in Contact, Your feedback will be appreciated.

Like/Follow @beatyourbit on FacebookTwitterInstagram & Telegram for latest updates.

128×64 SSD1306 OLED interface with Arduino (SPI)

Visual interface plays an important role in any embedded or electronics design. It unveils the seamless capabilities of your design by providing visual monitoring. In market, there are varieties of displays are available as per requirement. In earlier tutorial we learned how to interface 16×2 LCD with Arduino. Here in this, we will learn about OLED & it’s interface with Arduino.

OLED-organic light-emitting diode is a type of display which is used in devices such as television screens, computer monitors, portable systems such as smartphones, handheld game consoles, smart bands and PDAs. In OLED, the emissive electroluminescent layer is a film of organic compound that emits light in response to an electric current. This organic layer is situated between two electrodes; typically, at least one of these electrodes is transparent.

An OLED display works without a backlight because it emits visible light. Thus, it can display deep black levels and can be thinner and lighter than a liquid crystal display (LCD). In low ambient light conditions (such as a dark room), an OLED screen can achieve a higher contrast ratio than an LCD, regardless of whether the LCD uses cold cathode fluorescent lamps or an LED backlight. OLED displays are made in the same way as LCDs, but after TFT (for active matrix displays), addressable grid (for passive matrix displays) or ITO segment (for segment displays) formation, the display is coated with hole injection, transport and blocking layers, as well with electroluminescent material after the 2 first layers, after which ITO or metal may be applied again as a cathode and later the entire stack of materials is encapsulated. OLEDs have large fields of view, about 170 degrees. The TFT layer, addressable grid or ITO segments serve as or are connected to the anode, which may be made of ITO or metal. OLEDs can be made flexible and transparent, with transparent displays being used in smartphones with optical fingerprint scanners and flexible displays being used in foldable smartphones

Most common OLED available in market:

Here we will see 128×64 SSD1306 Monochrome OLED (SPI) interface with Arduino. It has 128×64 pixels & SSD1306 driver. It has >160° visual angle, operates on 3.3V ~ 6V & usually available in blue, white & dual color (some upper portion with different color). This OLED has SPI pins & it is mentioned below:

Sr. Pin Name Also known as Function
1 GND Ground   Ground pin of OLED
2 Vdd 5V, 3v3, Vcc   Power pin (3.3V to 5V)
3 SCK D0, CLK, SCL   Works as clock pin, It is used for both SPI & I2C
4 SDA MOSI, D1   Data pin of OLED, It is used for both SPI & I2C
5 RES RESET, RST   To reset OLED, part of SPI configuration
6 DC A0   Data Command pin, part of SPI configuration
7 CS Chip Select   Useful when we have multiple SPI module on same controller

Connection with Arduino

Arduino Pin OLED Pin
3.3V Vdd, Vcc, 3v3
Ground GND
11 DC, A0
12 CS, Chip Select


To getting started with this OLED, you will need libraries for it, here are two libraries recommended:

While using this OLED for the first time, it is recommended to test OLED & connections using examples in Arduino IDE. Go to File > Examples > Adafruit SSD1306 and select  ssd1306_128x64_spi example Upload the example and you can see line, drawing, text, scroll, invert display & snowflakes animation properly; if it is working fine means your connection & OLED both are fine.

Understand functions of this library

The library has various function for different output like start display, clear display, invert display, write text at particular location, show image & custom icons etc.

  • Start display & clear display:

OLED is like a writing pad, you have to inform it to display what you prepared. display.display() function is necessary after you cook something in program which is going to be display in next sequence.

If you write something again in same sketch to display, it will overwrite if you don’t clear the previous thing you displayed. display.clearDisplay() is required for the same.

  • Display a text string:

To display text or text string, you can use above lines, set x & y axis accordingly.

  • Display a value (character variable):

To display any character variable, for example temperature reading or any value, you can use above lines.

  • Draw some symbols like triangle, rectangle, circle:

Use any of the above functions to draw the element you want.

  • To invert color of whole display:

Using display.invertDisplay(true); function you can invert the display color (Black to white & vice versa), it will keep all image/text in inverse color until your code executes  display.invertDisplay(false);. 

Display image on OLED

To display an image on OLED display, the bitmap data must be stored in the program memory in form of PROMGMEM directive. In easy words, we have to instruct the OLED display what to do with each pixel by passing it a sequence or values from an array as shown above. This array will contain the bitmap data of the image. For this, you have to convert your B & W images into monochrome bitmap format with 128×64 pixels dimension. After that, open Image2cpp tool by Jasper van Loenen. This tool converts an image into a bit map values and load them into the array. Upload your 128×64 monochrome bitmap image there, check preview, review settings & check following setting;

Select code output format as ‘Arduino Code’ as shown in image, click ‘Generate code’ button. You will get a character set in window below ‘Generate code’ button. Copy it,  and paste in this code as character array;

Image display code

Select your Arduino board & port in Arduino IDE, upload the sketch & bingooo..!!! You will see the image on OLED display.

You can merge above mentioned functions and combine image with text & shapes. You can download following examples for better understanding.

Demo examples:

Here is one demo of OLED & Arduino:


I hope you will learn to drive your OLED display from this tutorial & it will be helpful to add visual enhancement in your DIY stuff. You can make various projects using such OLED where you need to display text/numbers & images, in future such projects will be uploaded in #BeatYourBit website. If you like this tutorial, share it with your friends & any Arduino newbie you know. Give your feedback about this tutorial in Contact, Your feedback will be appreciated.

Like/Follow @beatyourbit on FacebookTwitterInstagram & Telegram for latest updates.

16×2 LCD interface with Arduino

A liquid-crystal display (LCD) is a flat-panel display or other electronically modulated optical device that uses the light-modulating properties of liquid crystals. Liquid crystals do not emit light directly, instead using a back-light or reflector to produce images in color or monochrome. LCDs are available to display arbitrary images (as in a general-purpose computer display) or fixed images with low information content, which can be displayed or hidden, such as preset words, digits, and 7-segment displays, as in a digital clock. They use the same basic technology, except that arbitrary images are made up of a large number of small pixels, while other displays have larger elements. In this tutorial, we are using 16×2 character LCD display where 16 is characters per line & there are two lines. It means each 16×2 LCD can represent 32 characters, you can represent more characters by scrolling the text.


LCD-RearIn this LCD each character is displayed in 5×7 pixel matrix. This LCD has two registers, command & data. Like its name, Command register stores command sent to LCD & data register stores the data to be displayed on LCD. Basically the data is ASCII characters which is sent to be displayed on LCD. For more details about LCD, check here.


  • +Vcc: Supply voltage is 4.7V to 5.3V DC
  • GND: Ground (0V)
  • Contrast: Contrast adjustment of LCD, through variable resistor to +Vcc
  • RS: It means ‘Register Select’, Selects command register when low & command register when high
  • RW: It means ‘Read/Write’, Low to write the register and high to read from register
  • EN: It means ‘Enable’, sends data to data pins when a high to low pulse is given.
  • D0 to D7: 8 bit data pins
  • Back-light+ : Vcc (+5V) for LCD back-light
  • Back-light- : Ground (0V) for LCD back-light

Connection with Arduino UNO R3

There are two methods to interface 16×2 LCD with Arduino:

  1. Connect all pins of LCD to Arduino
  2. Use I2C module and connect to Arduino by two pins only

In first method, RS, R/W, EN and Data pins will be connected to Arduino;

As shown in above image, contrast pin is connected with pin 6 of Arduino hence contrast will be adjusted by value set in code. In the program ‘LiquidCrystal’ library so make sure you have same library installed in your Arduino IDE. (You can install libraries from Sketch>Include library>Manage libraries)

Code for direct connection method:

You can download above code from here.

In another method, an I2C module will be connected to LCD. By using I2C module only two pins SDA (Serial Data) & SCL (Serial Clock) will be used from Arduino so less wiring for this. LCD will receive commands from Arduino by I2C protocol

As shown in above image, I2C module is connected to LCD directly (For safer side, solder a female header strip on LCD and connect I2C on that so if somehow I2C module or LCD stops working then it can be easily replaced). You can connect multiple I2C device parallely on same pin. Actually A4 & A5 pins are SDA & SCL so same can be connected on A4 & A5 too(Check Arduino pinout first). There will be a jumper on I2C module which is used to turn on-off LCD backlight. You will find a potentiometer which is actually contrast set for LCD.

Code for I2C connection method:

You can download above code from here.

I2C module & its interface with LCD looks like this:

One more difference between both connection type is library. You need different library for I2C which is ‘LiquidCrystal_I2C’ and it is available here.

Before proceeding for I2C code download, you need to confirm I2C address. Each I2C module has an address which we need to mention in second line LiquidCrystal_I2C  lcd(0x3F, 16, 2); Here i’ve mentioned 0x3F because my I2C module has this address. To know your I2C module address you may need to run I2C scanner program in your Arduino and get I2C address on serial monitor like ths;

I2C scanner

Use this tutorial to know the address of your I2C device. Get I2C address, put in above code & eureka…!!!

After connecting LCD with Arduino you can display sensor & variable values, text messages & also can set values by interfacing keypad with Arduino. Give your feedback about this tutorial in Contact, Your feedback will be appreciated.

Join on FacebookTwitterInstagram & Telegram for latest updates.