Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Python to realize i2C in raspberry pie

Shulou Source: shulou.com Published: 2022-06-01 22:57:10 09月15日 Update

This article is about how raspberry pie uses Python to implement i2C. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

First of all, raspberry pie has to install python-smbus, i2c-tools.

Then modify the file: sudo nano / etc/modules, add the lines i2c-bcm2708 and i2c-dev, and Raspbian needs to activate i2c in raspi-config.

Check the device address with sudo i2cdetect-y 1

Example 1:LCD2004, device address is 0x27

First write a driver caller i2c_driver_lcd.py

Import smbusfrom time import * # LCD AddressADDRESS = 0x27# commandsLCD_CLEARDISPLAY = 0x01LCD_RETURNHOME = 0x02LCD_ENTRYMODESET = 0x04LCD_DISPLAYCONTROL = 0x08LCD_CURSORSHIFT = 0x10LCD_FUNCTIONSET = 0x20LCD_SETCGRAMADDR = 0x40LCD_SETDDRAMADDR = 0x80# flags for display entry modeLCD_ENTRYRIGHT = 0x00LCD_ENTRYLEFT = 0x02LCD_ENTRYSHIFTINCREMENT = 0x01LCD_ENTRYSHIFTDECREMENT = 0x00# flags for display on/off controlLCD_DISPLAYON = 0x04LCD_DISPLAYOFF = 0x00LCD_CURSORON = 0x02LCD_CURSOROFF = 0x00LCD_BLINKON = 0x01LCD_BLINKOFF = 0x00# flags for display/cursor shiftLCD_DISPLAYMOVE = 0x08LCD_CURSORMOVE = 0x00LCD_MOVERIGHT = 0x04LCD_MOVELEFT = 0x00# flags for function setLCD_8BITMODE = 0x10LCD_4BITMODE = 0x00LCD_2LINE = 0x08LCD_1LINE = 0x00LCD_5x10DOTS = 0x04LCD_5x8DOTS = 0x00# flags for backlight controlLCD_BACKLIGHT = 0x08LCD_NOBACKLIGHT = 0x00# set init LCD BACKLIGHT ON or OFFdef lcd_backlight (lcdbl=1): if lcdbl= = 0: return LCD_NOBACKLIGHT return LCD_BACKLIGHTEn = 0b00000100 # Enable bitRw = 0b00000010 # Read/Write bitRs = 0b00000001 # Register select bitclass lcd (object): # initializes objects and lcd def _ init__ (self Lcd_bl Port=1): self.addr = ADDRESS self.bus = smbus.SMBus (port) self.lcd_bl = lcd_bl self.lcd_write (0x03) self.lcd_write (0x02) self.lcd_write (LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE) Self.lcd_write (LCD_DISPLAYCONTROL | LCD_DISPLAYON) self.lcd_write (LCD_CLEARDISPLAY) self.lcd_write (LCD_ENTRYMODESET | LCD_ENTRYLEFT) sleep (0.2) def set_lcd (self Set_lcdbl): self.lcd_bl = set_lcdbl # clocks EN to latch command def lcd_strobe (self, data): self.write_cmd (data | En | lcd_backlight (self.lcd_bl)) sleep (.0005) self.write_cmd (data & ~ En) | lcd_backlight (self.lcd_bl)) sleep (.0001) def lcd_write_four_bits (self Data): self.write_cmd (data | lcd_backlight (self.lcd_bl)) self.lcd_strobe (data) # write a command to lcd def lcd_write (self, cmd, mode=0): self.lcd_write_four_bits (mode | (cmd & 0xF0)) self.lcd_write_four_bits (mode | (cmd)

Tags: Device raspberry address example content bus more program article power supply driver good practical two file article module activation look knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Shulou Technology Linux MariaDB