├── README.md ├── example.py └── lcd.py /README.md: -------------------------------------------------------------------------------- 1 | # micropython-lcd 2 | 3 | A simple class for controlling the HD44780 in 4 bit mode with the micropython 4 | pyboard. 5 | 6 | There are **no external dependences**, the only module we use is the pyboard's 7 | `pyb`. 8 | 9 | Micropython Forum thread: http://forum.micropython.org/viewtopic.php?f=2&t=354 10 | 11 | ## Usage 12 | 13 | ``` 14 | import pyb 15 | from lcd import HD44780 16 | 17 | lcd = HD44780() 18 | 19 | lcd.PINS = ['Y1','Y2','Y3','Y4','Y5','Y6'] 20 | 21 | lcd.init() 22 | 23 | lcd.set_line(0) # First line 24 | lcd.set_string("ABCDEFGHIJKLMNOP") # Send a string 25 | lcd.set_line(1) # Second line 26 | lcd.set_string("1234567890123456") # Again 27 | 28 | pyb.delay(3000) # 3 second delay 29 | 30 | lcd.clear() # Clear the display 31 | ``` 32 | 33 | ## Pinout 34 | 35 | Hookup your LCD like the following. 36 | 37 | ![Pinout photo][pinout] 38 | 39 | |Pin |Code |Description |Do what with it | 40 | |----|--------|--------------------|------------------------------------| 41 | |1 |VSS |GND |Ground it | 42 | |2 |VDD |+5V |5V please | 43 | |3 |V0 |Contrast (0-5V)* |Stick to 0V if you don't have a pot | 44 | |4 |RS |Register select |Connect to pyboard, 0 in array | 45 | |5 |R/W |Read/write |Ground it | 46 | |6 |E |Enable |Connect to pyboard, 1 in array | 47 | |7 |DB0 |Data Bit 0 |Unused | 48 | |8 |DB1 |Data Bit 1 |Unused | 49 | |9 |DB2 |Data Bit 2 |Unused | 50 | |10 |DB3 |Data Bit 3 |Unused | 51 | |11 |DB4 |Data Bit 4 |Connect to pyboard, 2 in array | 52 | |12 |DB5 |Data Bit 5 |Connect to pyboard, 3 in array | 53 | |13 |DB6 |Data Bit 6 |Connect to pyboard, 4 in array | 54 | |14 |DB7 |Data Bit 7 |Connect to pyboard, 5 in array | 55 | |15 |A |Backlight +someV |My display is LED, I use 3.3V | 56 | |16 |K |Backlight GND |Ground it | 57 | 58 | Then the pinout array looks like this 59 | 60 | PINS = ['Y1','Y2','Y3','Y4','Y5','Y6'] 61 | 62 | [pinout]:http://cdn.instructables.com/FMC/PZRT/G8LWOHW9/FMCPZRTG8LWOHW9.MEDIUM.jpg 63 | -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | # Example on usage of the HD44780 module 2 | 3 | # Pin Code Description Do what with it 4 | # ------------------------------------------------------------------- 5 | # 1 VSS GND Ground it 6 | # 2 VDD +5V 5V please 7 | # 3 V0 Contrast (0-5V)* Stick to 0V if you don't have a pot 8 | # 4 RS Register select Connect to pyboard, 0 in array 9 | # 5 R/W Read/write Ground it 10 | # 6 E Enable Connect to pyboard, 1 in array 11 | # 7 DB0 Data Bit 0 Unused 12 | # 8 DB1 Data Bit 1 Unused 13 | # 9 DB2 Data Bit 2 Unused 14 | # 10 DB3 Data Bit 3 Unused 15 | # 11 DB4 Data Bit 4 Connect to pyboard, 2 in array 16 | # 12 DB5 Data Bit 5 Connect to pyboard, 3 in array 17 | # 13 DB6 Data Bit 6 Connect to pyboard, 4 in array 18 | # 14 DB7 Data Bit 7 Connect to pyboard, 5 in array 19 | # 15 A Backlight +someV My display is LED, I use 3.3V 20 | # 16 K Backlight GND Ground it 21 | 22 | 23 | import pyb 24 | from lcd import HD44780 25 | 26 | def lcd_fun(): 27 | # Main program block 28 | lcd = HD44780() 29 | 30 | # Pins 0-5 as above 31 | lcd.PINS = ['Y1','Y2','Y3','Y4','Y5','Y6'] 32 | 33 | # Initialise display 34 | lcd.init() 35 | 36 | # Use it 37 | lcd.set_line(0) # First line 38 | lcd.set_string("ABCDEFGHIJKLMNOP") # Send a string 39 | lcd.set_line(1) # Second line 40 | lcd.set_string("1234567890123456") # Again 41 | 42 | pyb.delay(3000) # 3 second delay 43 | 44 | # Send some more 45 | lcd.set_line(0) 46 | lcd.set_string("*micropython-lcd") 47 | lcd.set_line(1) 48 | lcd.set_string("github.com/wjdp") 49 | 50 | pyb.delay(3000) 51 | 52 | # Done 53 | lcd.clear() 54 | -------------------------------------------------------------------------------- /lcd.py: -------------------------------------------------------------------------------- 1 | # HD44780 class for micropython board (http://micropython.org) 2 | # Written by Will Pimblett, based on http://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/ 3 | # http://github.com/wjdp/micropython-lcd 4 | # http://wjdp.co.uk 5 | 6 | import pyb 7 | 8 | class HD44780(object): 9 | # Pinout, change within or outside class for your use case 10 | PINS = ['Y1','Y2','Y3','Y4','Y5','Y6'] 11 | # Pin names, don't change 12 | PIN_NAMES = ['RS','E','D4','D5','D6','D7'] 13 | 14 | # Dict of pins 15 | pins = {} 16 | 17 | # Pin mode, push-pull control 18 | PIN_MODE = pyb.Pin.OUT_PP 19 | 20 | # Define some device constants 21 | LCD_WIDTH = 16 # Maximum characters per line 22 | # Designation of T/F for character and command modes 23 | LCD_CHR = True 24 | LCD_CMD = False 25 | 26 | LINES = { 27 | 0: 0x80, # LCD RAM address for the 1st line 28 | 1: 0xC0, # LCD RAM address for the 2nd line 29 | # Add more if desired 30 | } 31 | 32 | # Timing constants 33 | E_PULSE = 50 34 | E_DELAY = 50 35 | 36 | def init(self): 37 | # Initialise pins 38 | for pin, pin_name in zip(self.PINS, self.PIN_NAMES): 39 | # setattr(self, 'LCD_'+pin_name, # Unsupported 40 | # pyb.Pin(pin, self.PIN_MODE)) 41 | self.pins['LCD_'+pin_name] = pyb.Pin(pin, self.PIN_MODE) 42 | # Initialise display 43 | self.lcd_byte(0x33,self.LCD_CMD) 44 | self.lcd_byte(0x32,self.LCD_CMD) 45 | self.lcd_byte(0x28,self.LCD_CMD) 46 | self.lcd_byte(0x0C,self.LCD_CMD) 47 | self.lcd_byte(0x06,self.LCD_CMD) 48 | self.lcd_byte(0x01,self.LCD_CMD) 49 | 50 | def clear(self): 51 | # Clear the display 52 | self.lcd_byte(0x01,self.LCD_CMD) 53 | 54 | def set_line(self, line): 55 | # Set the line that we're going to print to 56 | self.lcd_byte(self.LINES[line], self.LCD_CMD) 57 | 58 | def set_string(self, message): 59 | # Pad string out to LCD_WIDTH 60 | # message = message.ljust(LCD_WIDTH," ") 61 | m_length = len(message) 62 | if m_length < self.LCD_WIDTH: 63 | short = self.LCD_WIDTH - m_length 64 | blanks=str() 65 | for i in range(short): 66 | blanks+=' ' 67 | message+=blanks 68 | for i in range(self.LCD_WIDTH): 69 | self.lcd_byte(ord(message[i]), self.LCD_CHR) 70 | 71 | def lcd_byte(self, bits, mode): 72 | # Send byte to data pins 73 | # bits = data 74 | # mode = True for character 75 | # False for command 76 | 77 | self.pin_action('LCD_RS', mode) # RS 78 | 79 | # High bits 80 | self.pin_action('LCD_D4', False) 81 | self.pin_action('LCD_D5', False) 82 | self.pin_action('LCD_D6', False) 83 | self.pin_action('LCD_D7', False) 84 | if bits&0x10==0x10: 85 | self.pin_action('LCD_D4', True) 86 | if bits&0x20==0x20: 87 | self.pin_action('LCD_D5', True) 88 | if bits&0x40==0x40: 89 | self.pin_action('LCD_D6', True) 90 | if bits&0x80==0x80: 91 | self.pin_action('LCD_D7', True) 92 | 93 | # Toggle 'Enable' pin 94 | self.udelay(self.E_DELAY) 95 | self.pin_action('LCD_E', True) 96 | self.udelay(self.E_PULSE) 97 | self.pin_action('LCD_E', False) 98 | self.udelay(self.E_DELAY) 99 | 100 | # Low bits 101 | self.pin_action('LCD_D4', False) 102 | self.pin_action('LCD_D5', False) 103 | self.pin_action('LCD_D6', False) 104 | self.pin_action('LCD_D7', False) 105 | if bits&0x01==0x01: 106 | self.pin_action('LCD_D4', True) 107 | if bits&0x02==0x02: 108 | self.pin_action('LCD_D5', True) 109 | if bits&0x04==0x04: 110 | self.pin_action('LCD_D6', True) 111 | if bits&0x08==0x08: 112 | self.pin_action('LCD_D7', True) 113 | 114 | # Toggle 'Enable' pin 115 | self.udelay(self.E_DELAY) 116 | self.pin_action('LCD_E', True) 117 | self.udelay(self.E_PULSE) 118 | self.pin_action('LCD_E', False) 119 | self.udelay(self.E_DELAY) 120 | 121 | def udelay(self, us): 122 | # Delay by us microseconds, set as function for portability 123 | pyb.udelay(us) 124 | 125 | def pin_action(self, pin, high): 126 | # Pin high/low functions, set as function for portability 127 | if high: 128 | self.pins[pin].high() 129 | else: 130 | self.pins[pin].low() 131 | --------------------------------------------------------------------------------