2 Digit LED 2015 Adrian Krag Objective This document explores using the MCP23017, the port expander to create a 2 digit LED display. An LED character is different from an LCD. The LCD has a built in controller that can write both characters and letters. Sometimes that's more than is needed. If the project calls for only a simple 2 digit number this approach can be very inexpensive and very easy to implement. A 7 Segment digit requires 8 digital outputs (7 segments and a decimal point). Two digits would require more pins than are available on the standard Arduino. To solve this issue, the MCP23017 is a very useful device. This expander chip has 16 digital pins in 2 - 8 bit ports, one to control each digit. Writing Digits to the 7 Segment LED The LED has 10 pins, 8 for the elements and 2 ground pins. Each element pin can turn on one segment. To write a number to the display, the program writes one byte, 8 bits. If the individual bit is a 1, the segment is on. 0 turns it off. The segments are wired to the port pins. Pins 21 to 28, General Purpose Port A are connected to the right digit. Pins 0 to 8 are connected to the left digit. To create a digit the program just has to sets and clears the appropriate bits. Careful analysis of the circuit will tell which bit in the Expander port activates which segment. However, a little experimentation with different words will also give us the information. bit 0 1 2 3 4 5 6 7 Seg g f a b o c d e The segment on our board is set as shown in these tables. To Write numbers use the segment, use the following binary numbers. For example to turn on a 0 - segment f, a, b, c, d, e To Create a digit select the segments to turn on, and set the corresponding bits in the port corresponding with the correct bits. 11101110B 00101000B 11001101B 01101101B 00101011B 01100111B 11100111B 00101100B 11101111B 00101111B =0 =1 =2 =3 =4 =5 =6 =7 =8 =9 One way to do this would be to create an array of binary numbers that would correspond to each digit. Begin in this program with the "dgt()" subroutine on line 21 of this sketch. The MCP23017 expander chip has registers. Writing to these Registers controls the operation of the of the device. This subroutine will send a byte (calling parameter N) to one of the registers (calling parameter Reg.) Below is a table of the registers in the Chip. Register 0x00 and 0x01 determine which pins in port A or B respectively. Each bit corresponds to a pin in the port. For this all the port pins must be OUTPUT. To make them such the program writes 0x00 to each of these registers. Just as the pinMode() instruction from the Arduino this does not set the values, it only makes the pins OUTPUT. The equivalent of the digitalWrite() instruction requires writing to GPIOA and GPIOB registers 0x12 and 0x13. Each of the other registers performs some other alteration of the Registers. A complete listing of the functions of these control registers can be found in the data sheet for the chip. Registers 0x0C and 0x0D, the GPPUA and GPPUB turn on the internal pull up resistors for the pins that have been configured as inputs. There is more information on the MCP23017 in the lecture material "Advanced Arduino Programming 2014 Conference." More Things About the Counting Program First the global nmbr[] array. This set of binary numbers will turn on the segments to make the numbers that corresponding to the position in the array - the 0th element is 0. Element number 6 is 6 and so on. The setup() routine initializes the I2C (Wire.begin()), then sets the IODIRA and IODIRB registers in the expander chip. In the loop() function the digits are calculated and the routine sequences through the numbers 00 to 99. The counting variable 'i' is divided by 10. The ones digit is the modulo of the counting variable and 10. This is a simple demonstration of what can be done with the expander and couple cheap 7Segment digits. It's interesting to note that the data sheet says that the expander can put out 25mA per pin. This is not enough to burn up most LEDs. This means that series resistors are not essential. However, it is always a good idea to control the current through the LED and that means good design practice to include them. Let's look at the schematic for the expander board. The GP pins are connected to the control pins through 150 ohm resistors. This schematic is converted into a board layout. In this the blue traces are on the bottom. Red is on the top. This converts to a small circuit board. that creates the electrical connections this fits nicely into a small box that can be designed and printed. Conclusions This makes a really nice little display if the project needs something like this. As always, if there are any questions, feel free to contact me. ak@thecije.com