Python busio.I2C Examples
The following are 30
code examples of busio.I2C().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
busio
, or try the search function
.
Example #1
Source File: character_lcd.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def clear(self): """Clears everything displayed on the LCD. The following example displays, "Hello, world!", then clears the LCD. .. code-block:: python import time import board import busio import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" time.sleep(5) lcd.clear() """ self._write8(_LCD_CLEARDISPLAY) time.sleep(0.003)
Example #2
Source File: core.py From terkin-datalogger with GNU Affero General Public License v3.0 | 6 votes |
def setup_buses(self, buses_settings): """Register configured I2C, OneWire and SPI buses. :param buses_settings: """ effective_buses = [] effective_bus_ids = [] for bus_settings in buses_settings: if bus_settings.get("enabled"): effective_buses.append(bus_settings) effective_bus_ids.append(bus_settings['id']) log.info('Starting buses: %s', effective_bus_ids) for bus_settings in effective_buses: try: self.setup_bus(bus_settings) except Exception as ex: log.exc(ex, 'Registering bus failed. settings={}'.format(bus_settings))
Example #3
Source File: log_si7021.py From Pigrow with GNU General Public License v3.0 | 6 votes |
def read_sensor(sensor): try: i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_si7021.SI7021(i2c) temperature = sensor.temperature humidity = sensor.relative_humidity if humidity == None or temperature == None or humidity > 101: print("--problem reading sensor on GPIO:"+sensor_gpio+"--") return '-1','-1','-1' else: humidity = round(humidity,2) temperature = round(temperature, 2) logtime = datetime.datetime.now() return humidity, temperature, logtime except: print("--problem reading si7021") return '-1','-1','-1'
Example #4
Source File: i2c_device.py From Adafruit_CircuitPython_BusDevice with MIT License | 6 votes |
def __probe_for_device(self): """ Try to read a byte from an address, if you get an OSError it means the device is not there or that the device does not support these means of probing """ while not self.i2c.try_lock(): pass try: self.i2c.writeto(self.device_address, b"") except OSError: # some OS's dont like writing an empty bytesting... # Retry by reading a byte try: result = bytearray(1) self.i2c.readfrom_into(self.device_address, result) except OSError: raise ValueError("No I2C device at address: %x" % self.device_address) finally: self.i2c.unlock()
Example #5
Source File: i2c_device.py From Adafruit_CircuitPython_BusDevice with MIT License | 6 votes |
def write(self, buf, *, start=0, end=None, stop=True): """ Write the bytes from ``buffer`` to the device. Transmits a stop bit if ``stop`` is set. If ``start`` or ``end`` is provided, then the buffer will be sliced as if ``buffer[start:end]``. This will not cause an allocation like ``buffer[start:end]`` will so it saves memory. :param bytearray buffer: buffer containing the bytes to write :param int start: Index to start writing from :param int end: Index to read up to but not include; if None, use ``len(buf)`` :param bool stop: If true, output an I2C stop condition after the buffer is written """ if end is None: end = len(buf) self.i2c.writeto(self.device_address, buf, start=start, end=end, stop=stop) # pylint: disable-msg=too-many-arguments
Example #6
Source File: character_lcd_rgb_i2c.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def select_button(self): """The select button on the RGB Character LCD I2C Shield or Pi plate. The following example prints "Select!" to the LCD when the select button is pressed: .. code-block:: python import board import busio from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C i2c = busio.I2C(board.SCL, board.SDA) lcd = Character_LCD_RGB_I2C(i2c, 16, 2) while True: if lcd.select_button: lcd.message = "Select!" """ return not self._select_button.value
Example #7
Source File: character_lcd_rgb_i2c.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def right_button(self): """The right button on the RGB Character LCD I2C Shield or Pi plate. The following example prints "Right!" to the LCD when the right button is pressed: .. code-block:: python import board import busio from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C i2c = busio.I2C(board.SCL, board.SDA) lcd = Character_LCD_RGB_I2C(i2c, 16, 2) while True: if lcd.right_button: lcd.message = "Right!" """ return not self._right_button.value
Example #8
Source File: character_lcd_rgb_i2c.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def down_button(self): """The down button on the RGB Character LCD I2C Shield or Pi plate. The following example prints "Down!" to the LCD when the down button is pressed: .. code-block:: python import board import busio from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C i2c = busio.I2C(board.SCL, board.SDA) lcd = Character_LCD_RGB_I2C(i2c, 16, 2) while True: if lcd.down_button: lcd.message = "Down!" """ return not self._down_button.value
Example #9
Source File: character_lcd_rgb_i2c.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def up_button(self): """The up button on the RGB Character LCD I2C Shield or Pi plate. The following example prints "Up!" to the LCD when the up button is pressed: .. code-block:: python import board import busio from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C i2c = busio.I2C(board.SCL, board.SDA) lcd = Character_LCD_RGB_I2C(i2c, 16, 2) while True: if lcd.up_button: lcd.message = "Up!" """ return not self._up_button.value
Example #10
Source File: character_lcd_rgb_i2c.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def left_button(self): """The left button on the RGB Character LCD I2C Shield or Pi plate. The following example prints "Left!" to the LCD when the left button is pressed: .. code-block:: python import board import busio from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C i2c = busio.I2C(board.SCL, board.SDA) lcd = Character_LCD_RGB_I2C(i2c, 16, 2) while True: if lcd.left_button: lcd.message = "Left!" """ return not self._left_button.value
Example #11
Source File: character_lcd.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def text_direction(self): """The direction the text is displayed. To display the text left to right beginning on the left side of the LCD, set ``text_direction = LEFT_TO_RIGHT``. To display the text right to left beginning on the right size of the LCD, set ``text_direction = RIGHT_TO_LEFT``. Text defaults to displaying from left to right. The following example displays "Hello, world!" from right to left. .. code-block:: python import time import board import busio import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.text_direction = lcd.RIGHT_TO_LEFT lcd.message = "Hello, world!" time.sleep(5) """ return self._direction
Example #12
Source File: character_lcd.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def move_right(self): """Moves displayed text right one column. The following example scrolls a message to the right off the screen. .. code-block:: python import time import board import busio import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) scroll_message = "Scroll -->" lcd.message = scroll_message time.sleep(2) for i in range(len(scroll_message) + 16): lcd.move_right() time.sleep(0.5) """ self._write8(_LCD_CURSORSHIFT | _LCD_DISPLAYMOVE | _LCD_MOVERIGHT)
Example #13
Source File: character_lcd.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def message(self): """Display a string of text on the character LCD. Start position is (0,0) if cursor_position is not set. If cursor_position is set, message starts at the set position from the left for left to right text and from the right for right to left text. Resets cursor column and row to (0,0) after displaying the message. The following example displays, "Hello, world!" on the LCD. .. code-block:: python import time import board import busio import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" time.sleep(5) """ return self._message
Example #14
Source File: character_lcd.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def display(self): """ Enable or disable the display. True to enable the display. False to disable the display. The following example displays, "Hello, world!" on the LCD and then turns the display off. .. code-block:: python import time import board import busio import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.message = "Hello, world!" time.sleep(5) lcd.display = False """ return self.displaycontrol & _LCD_DISPLAYON == _LCD_DISPLAYON
Example #15
Source File: character_lcd.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def blink(self): """ Blink the cursor. True to blink the cursor. False to stop blinking. The following example shows a message followed by a blinking cursor for five seconds. .. code-block:: python import time import board import busio import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.blink = True lcd.message = "Blinky cursor!" time.sleep(5) lcd.blink = False """ return self.displaycontrol & _LCD_BLINKON == _LCD_BLINKON
Example #16
Source File: character_lcd_i2c.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def __init__(self, i2c, columns, lines, address=None, backlight_inverted=False): """Initialize character LCD connected to backpack using I2C connection on the specified I2C bus with the specified number of columns and lines on the display. Optionally specify if backlight is inverted. """ if address: mcp = MCP23008(i2c, address=address) else: mcp = MCP23008(i2c) super().__init__( mcp.get_pin(1), mcp.get_pin(2), mcp.get_pin(3), mcp.get_pin(4), mcp.get_pin(5), mcp.get_pin(6), columns, lines, backlight_pin=mcp.get_pin(7), backlight_inverted=backlight_inverted, )
Example #17
Source File: character_lcd.py From Adafruit_CircuitPython_CharLCD with MIT License | 6 votes |
def cursor(self): """True if cursor is visible. False to stop displaying the cursor. The following example shows the cursor after a displayed message: .. code-block:: python import time import board import busio import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.cursor = True lcd.message = "Cursor! " time.sleep(5) """ return self.displaycontrol & _LCD_CURSORON == _LCD_CURSORON
Example #18
Source File: snake_eyes_bonnet.py From Pi_Eyes with MIT License | 5 votes |
def __init__(self, *args, **kwargs): """SnakeEyesBonnet constructor.""" super(SnakeEyesBonnet, self).__init__(*args, **kwargs) # Thread self.i2c = busio.I2C(board.SCL, board.SDA) self.ads = ADS.ADS1015(self.i2c) self.ads.gain = 1 self.period = 1.0 / 60.0 # Polling inverval = 1/60 sec default self.print_values = False # Don't print values by default self.channel = [] for index in range(4): self.channel.append(AdcChannel( AnalogIn(self.ads, self.channel_dict[index])))
Example #19
Source File: core.py From terkin-datalogger with GNU Affero General Public License v3.0 | 5 votes |
def power_off(self): """ Turn off the I2C peripheral. https://docs.pycom.io/firmwareapi/pycom/machine/i2c.html """ log.info('Turning off I2C bus {}'.format(self.name)) if self.platform_info.vendor == self.platform_info.MICROPYTHON.Pycom: self.adapter.deinit()
Example #20
Source File: core.py From terkin-datalogger with GNU Affero General Public License v3.0 | 5 votes |
def power_on(self): """ Turn on the I2C peripheral after power off. """ # Don't reinitialize device if power on just occurred through initial driver setup. if self.just_started: self.just_started = False return # uPy doesn't have deinit so it doesn't need init if self.platform_info.vendor == self.platform_info.MICROPYTHON.Pycom: from machine import I2C self.adapter.init(mode=I2C.MASTER, baudrate=self.frequency)
Example #21
Source File: core.py From terkin-datalogger with GNU Affero General Public License v3.0 | 5 votes |
def scan_devices(self): log.info('Scan I2C with id={} bus for devices...'.format(self.number)) self.devices = self.adapter.scan() # i2c.readfrom(0x76, 5) log.info("Found {} I2C devices: {}".format(len(self.devices), self.devices))
Example #22
Source File: core.py From terkin-datalogger with GNU Affero General Public License v3.0 | 5 votes |
def setup_bus(self, bus_settings): """ :param bus_settings: """ bus_family = bus_settings.get('family') if bus_family == BusType.OneWire: owb = OneWireBus(bus_settings) if 'pin_data' in bus_settings: owb.register_pin("data", bus_settings['pin_data']) owb.start() self.register_bus(owb) elif bus_family == BusType.I2C: i2c = I2CBus(bus_settings) if 'pin_sda' in bus_settings: i2c.register_pin("sda", bus_settings['pin_sda']) if 'pin_scl' in bus_settings: i2c.register_pin("scl", bus_settings['pin_scl']) i2c.start() self.register_bus(i2c) else: log.error("Invalid bus configuration: %s", bus_settings)
Example #23
Source File: sensor_si7021.py From Pigrow with GNU General Public License v3.0 | 5 votes |
def read_sensor(location="", extra="", *args): # Try importing the modules then give-up and report to user if it fails import datetime try: import board import busio import adafruit_si7021 except: print("adafruit_si7021 module not installed, install using the command;") print(" sudo pip3 install adafruit-circuitpython-si7021") return None # set up and read the sensor try: i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_si7021.SI7021(i2c) temperature = sensor.temperature humidity = sensor.relative_humidity if humidity == None or temperature == None or humidity > 101: print("--problem reading si7021") return None else: humidity = round(humidity,2) temperature = round(temperature, 2) logtime = datetime.datetime.now() return [['time',logtime], ['humid', humidity], ['temperature', temperature]] except: print("--problem reading si7021") return None
Example #24
Source File: sensor_rgbtcs34725.py From Pigrow with GNU General Public License v3.0 | 5 votes |
def read_sensor(location="", extra="", *args): # Try importing the modules then give-up and report to user if it fails import datetime import time try: import board import busio import adafruit_tcs34725 except: print("adafruit_tcs34725 module not installed, install using the command;") print(" sudo pip3 install adafruit-circuitpython-tcs34725 ") return None # set up and read the sensor read_attempt = 1 i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_tcs34725.TCS34725(i2c) gain = 1 sensor.gain = gain # 1, 4, 16, 60 sensor.integration_time = 50 # The integration time of the sensor in milliseconds. Must be a value between 2.4 and 614.4. while read_attempt < 5: try: color_temp = sensor.color_temperature lux = sensor.lux rgb = sensor.color_rgb_bytes # if lux == None: print("--problem reading tcs34725, try " + str(read_attempt)) time.sleep(2) read_attempt = read_attempt + 1 else: logtime = datetime.datetime.now() return [['time',logtime], ['lux', str(lux / gain)], ['color_temp', color_temp], ['r', rgb[0]], ['g', rgb[1]], ['b', rgb[2]]] except Exception as e: print("--exception while reading tcs34725, try " + str(read_attempt)) print(" -- " + str(e)) time.sleep(2) read_attempt = read_attempt + 1 return None
Example #25
Source File: uv.py From aws-builders-fair-projects with Apache License 2.0 | 5 votes |
def __init__(self): Sensor.__init__(self) self.uv_min = float(self.config["uv"]["min"]) self.uv_max = float(self.config["uv"]["max"]) # initialize I2C i2c = busio.I2C(board.SCL, board.SDA) # create the VEML6075 object self.veml = adafruit_veml6075.VEML6075( i2c, integration_time=int(self.config["uv"]["integration_time"]) )
Example #26
Source File: board.py From Adafruit_Blinka with MIT License | 5 votes |
def I2C(): """The singleton I2C interface""" import busio return busio.I2C(SCL, SDA)
Example #27
Source File: character_lcd.py From Adafruit_CircuitPython_CharLCD with MIT License | 5 votes |
def backlight(self): """Enable or disable backlight. True if backlight is on. False if backlight is off. The following example turns the backlight off, then displays, "Hello, world?", then turns the backlight on and displays, "Hello, world!" .. code-block:: python import time import board import busio import adafruit_character_lcd.character_lcd_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) lcd = character_lcd.Character_LCD_I2C(i2c, 16, 2) lcd.backlight = False lcd.message = "Hello, world?" time.sleep(5) lcd.backlight = True lcd.message = "Hello, world!" time.sleep(5) """ return self._enable
Example #28
Source File: character_lcd.py From Adafruit_CircuitPython_CharLCD with MIT License | 5 votes |
def color(self): """ The color of the display. Provide a list of three integers ranging 0 - 100, ``[R, G, B]``. ``0`` is no color, or "off". ``100`` is maximum color. For example, the brightest red would be ``[100, 0, 0]``, and a half-bright purple would be, ``[50, 0, 50]``. If PWM is unavailable, ``0`` is off, and non-zero is on. For example, ``[1, 0, 0]`` would be red. The following example turns the LCD red and displays, "Hello, world!". .. code-block:: python import time import board import busio import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd i2c = busio.I2C(board.SCL, board.SDA) lcd = character_lcd.Character_LCD_RGB_I2C(i2c, 16, 2) lcd.color = [100, 0, 0] lcd.message = "Hello, world!" time.sleep(5) """ return self._color
Example #29
Source File: sensor_bme280.py From Pigrow with GNU General Public License v3.0 | 4 votes |
def read_sensor(location="", extra="", *args): # Try importing the modules then give-up and report to user if it fails import datetime import time try: import board import busio import adafruit_bme280 except: print("bme280 module not installed, install using the command;") print(" sudo pip3 install adafruit-circuitpython-bme280 ") return None # set up and read the sensor read_attempt = 1 i2c = busio.I2C(board.SCL, board.SDA) i2c_address = location if location == "0x76": bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x76) elif location == "0x77": bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x77) while read_attempt < 5: try: temperature = bme280.temperature humidity = bme280.humidity pressure = bme280.pressure # if humidity == None or temperature == None or humidity > 101: print("--problem reading bme280, try " + str(read_attempt)) time.sleep(2) read_attempt = read_attempt + 1 else: humidity = round(humidity,2) temperature = round(temperature, 2) pressure = round(pressure, 2) logtime = datetime.datetime.now() return [['time',logtime], ['humid', humidity], ['temperature', temperature], ['pressure', pressure]] except Exception as e: print("--exception while reading bcm280, try " + str(read_attempt)) print(" -- " + str(e)) time.sleep(2) read_attempt = read_attempt + 1 return None
Example #30
Source File: circuit_playground_base.py From Adafruit_CircuitPython_CircuitPlayground with MIT License | 4 votes |
def __init__(self): self._a = digitalio.DigitalInOut(board.BUTTON_A) self._a.switch_to_input(pull=digitalio.Pull.DOWN) self._b = digitalio.DigitalInOut(board.BUTTON_B) self._b.switch_to_input(pull=digitalio.Pull.DOWN) self.gamepad = gamepad.GamePad(self._a, self._b) # Define switch: self._switch = digitalio.DigitalInOut(board.SLIDE_SWITCH) self._switch.switch_to_input(pull=digitalio.Pull.UP) # Define LEDs: self._led = digitalio.DigitalInOut(board.D13) self._led.switch_to_output() self._pixels = neopixel.NeoPixel(board.NEOPIXEL, 10) # Define sensors: self._temp = adafruit_thermistor.Thermistor( board.TEMPERATURE, 10000, 10000, 25, 3950 ) self._light = Photocell(board.LIGHT) # Define touch: # Initially, self._touches stores the pin used for a particular touch. When that touch is # used for the first time, the pin is replaced with the corresponding TouchIn object. # This saves a little RAM over using a separate read-only pin tuple. # For example, after `cp.touch_A2`, self._touches is equivalent to: # [None, board.A1, touchio.TouchIn(board.A2), board.A3, ...] # Slot 0 is not used (A0 is not allowed as a touch pin). self._touches = [ None, board.A1, board.A2, board.A3, board.A4, board.A5, board.A6, board.TX, ] self._touch_threshold_adjustment = 0 # Define acceleration: self._i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) self._int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT) self._lis3dh = adafruit_lis3dh.LIS3DH_I2C( self._i2c, address=0x19, int1=self._int1 ) self._lis3dh.range = adafruit_lis3dh.RANGE_8_G # Define audio: self._speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) self._speaker_enable.switch_to_output(value=False) self._sample = None self._sine_wave = None self._sine_wave_sample = None # Initialise tap: self._detect_taps = 1 self.detect_taps = 1