About Us Knowledge Base Community Forum Electronics Toolbox FAQ Shipping & Delivery

Community Forum

Share your projects and post your questions

Search
Register or Sign In
Forum

I2C Switch

79 Views - Created 32 days ago

32 days ago

Posted by:
Ermahiho

Last edited: 26/09/2020

Users Avatar

Hello everyone, i'm trying to use I2C Switch on my Raspberry Pi 0 WH and by usign the Python library

https://github.com/abelectronicsuk/ABElectronics_Python_Libraries/tree/master/I2CSwitch

I'm able to set the channel 1,2,3,4 but now the main issue that i have is how to read the value on my BH1750 sensors connected to the 4 channels available on the board. With the previous code i was able to read data from my sensor directly connected to the GPIO of the Raspebrry but now i need to pass through the board and after set channel my previous code not work and show error in connection.

How i can ask, one by one, the value of sensors passing through switch board?

Thanks in advance

Here is the code that works perfectly when sensor is connected directly ithout board :

#!/usr/bin/python

import smbus

import time

import requests

 

# Define some constants from the datasheet

DEVICE     = 0x23 # Default device I2C address

POWER_DOWN = 0x00 # No active state

POWER_ON   = 0x01 # Power on

RESET      = 0x07 # Reset data register value

ONE_TIME_HIGH_RES_MODE = 0x20

bus = smbus.SMBus(1) # Rev 2 Pi uses 1

 

def convertToNumber(data):

# Simple function to convert 2 bytes of data

# into a decimal number

  return ((data[1] + (256 * data[0])) / 1.2)

 

def readLight(addr=DEVICE):

  data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE)

  return convertToNumber(data)

 

def main():

  while True:

    #print ("Light Level : " + str(readLight()) + " lux")

    payload = {'door_psw':'YLGC202008','id_procedure':'0','id_door':'1','external_lux':str(readLight()),'internal_lux':str(readLight())}

    #print(payload);

    r = requests.post('http://192.168.1.14/take_params.php', data=payload)

    if r.ok :

        if r.json()["data"]["response"] == "OK":

            print("Dati lux aggiornati");

    time.sleep(0.5)

 

if __name__=="__main__":

   main()

31 days ago

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

Hello

The code below should switch between each I2C channel and read the sensor values.

I added a channel parameter into the the readLight function and used this to set the i2c switch channel.  There is a short delay added after switching the i2c channel, this may not be needed.

In the main loop I duplicated the payload variable and added the channel parameter to each readLight function call.

This should give you an idea of how you can use the I2C switch in your code.

#!/usr/bin/python

import smbus
import time
import requests
from I2CSwitch import I2CSwitch
 
# Define some constants from the datasheet
DEVICE     = 0x23 # Default device I2C address
POWER_DOWN = 0x00 # No active state
POWER_ON   = 0x01 # Power on
RESET      = 0x07 # Reset data register value
ONE_TIME_HIGH_RES_MODE = 0x20
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
i2cswitch = I2CSwitch(0x70) # I2C switch on address 0x70

    
def convertToNumber(data):
# Simple function to convert 2 bytes of data
# into a decimal number
  return ((data[1] + (256 * data[0])) / 1.2)

def readLight(channel=1, addr=DEVICE):
  # Set the I2C channel
  i2cswitch.switch_channel(channel)
  time.sleep(0.1)
  data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE)
  return convertToNumber(data)

def main():
  i2cswitch.reset()

  while True:
    #print ("Light Level : " + str(readLight()) + " lux")
    payload1 = {'door_psw':'YLGC202008','id_procedure':'0','id_door':'1','external_lux':str(readLight()),'internal_lux':str(readLight(channel=1))}

    payload2 = {'door_psw':'YLGC202008','id_procedure':'0','id_door':'1','external_lux':str(readLight()),'internal_lux':str(readLight(channel=2))}

    payload3 = {'door_psw':'YLGC202008','id_procedure':'0','id_door':'1','external_lux':str(readLight()),'internal_lux':str(readLight(channel=3))}

    payload4 = {'door_psw':'YLGC202008','id_procedure':'0','id_door':'1','external_lux':str(readLight()),'internal_lux':str(readLight(channel=4))}
    #print(payload);

    r = requests.post('http://192.168.1.14/take_params.php', data=payload1)

    if r.ok :
      if r.json()["data"]["response"] == "OK":
        print("Dati lux aggiornati")

      time.sleep(0.5)

if __name__=="__main__":
   main()

 

AB Electronics UK Tech Support

30 days ago

Posted by:
Ermahiho

Users Avatar

It works! thank you very much!

30 days ago

Posted by:
Ermahiho

Users Avatar

i have a little question for you, i can setup delay 0.1 without problem but because i need to switch between channel 1 and 2 every 1s 24/24 7/7 365/365 this can broke the board/chip or is strong and will work without problem? what do you think?

30 days ago

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

It should work without the delay.  I only added it in to make sure the switch had changed channel before doing the i2c read but it probably isn't needed.

Try removing the delay and see if it still works correctly.

AB Electronics UK Tech Support

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view, download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.