Electronics- Serial Communication-I2Cdenethor.wlu.ca/pc320/lectures/i2cbeam.pdfRaspberry Pi Python...

of 51 /51
Serial Communication -I2C Raspberry Pi Python I2C Library Electronics Serial Communication-I2C Terry Sturtevant Wilfrid Laurier University February 13, 2019 Terry Sturtevant Electronics Serial Communication-I2C

Embed Size (px)

Transcript of Electronics- Serial Communication-I2Cdenethor.wlu.ca/pc320/lectures/i2cbeam.pdfRaspberry Pi Python...

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    ElectronicsSerial Communication-I2C

    Terry Sturtevant

    Wilfrid Laurier University

    February 13, 2019

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Serial Communication -I2C

    Inter-Integrated Circuit InterfaceMaster/slave communicationUses 2 signals (and Ground),SDA and SCLMany slaves can be on the same bus since each has an addressDevice addresses are pre-programmed, but can usually bechangedSynchronous, so master controls clock rate

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Serial Communication -I2C

    Inter-Integrated Circuit Interface

    Master/slave communicationUses 2 signals (and Ground),SDA and SCLMany slaves can be on the same bus since each has an addressDevice addresses are pre-programmed, but can usually bechangedSynchronous, so master controls clock rate

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Serial Communication -I2C

    Inter-Integrated Circuit InterfaceMaster/slave communication

    Uses 2 signals (and Ground),SDA and SCLMany slaves can be on the same bus since each has an addressDevice addresses are pre-programmed, but can usually bechangedSynchronous, so master controls clock rate

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Serial Communication -I2C

    Inter-Integrated Circuit InterfaceMaster/slave communicationUses 2 signals (and Ground),SDA and SCL

    Many slaves can be on the same bus since each has an addressDevice addresses are pre-programmed, but can usually bechangedSynchronous, so master controls clock rate

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Serial Communication -I2C

    Inter-Integrated Circuit InterfaceMaster/slave communicationUses 2 signals (and Ground),SDA and SCLMany slaves can be on the same bus since each has an address

    Device addresses are pre-programmed, but can usually bechangedSynchronous, so master controls clock rate

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Serial Communication -I2C

    Inter-Integrated Circuit InterfaceMaster/slave communicationUses 2 signals (and Ground),SDA and SCLMany slaves can be on the same bus since each has an addressDevice addresses are pre-programmed, but can usually bechanged

    Synchronous, so master controls clock rate

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Serial Communication -I2C

    Inter-Integrated Circuit InterfaceMaster/slave communicationUses 2 signals (and Ground),SDA and SCLMany slaves can be on the same bus since each has an addressDevice addresses are pre-programmed, but can usually bechangedSynchronous, so master controls clock rate

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Bidirectional Communication on a Single Line

    How can multiple devices communicate on the same line?Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.Any device which is not speaking must let their output float.Only one device can pull the line low at a time.

    On the Raspberry Pi board, there are 1.8kΩ pull-up resistorsto 3.3V, so external ones are not normally required.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Bidirectional Communication on a Single Line

    How can multiple devices communicate on the same line?

    Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.Any device which is not speaking must let their output float.Only one device can pull the line low at a time.

    On the Raspberry Pi board, there are 1.8kΩ pull-up resistorsto 3.3V, so external ones are not normally required.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Bidirectional Communication on a Single Line

    How can multiple devices communicate on the same line?Each device has an open-collector (or open-drain) output.

    A pull-up resistor on the line allows it to go high whennothing is pulling it low.Any device which is not speaking must let their output float.Only one device can pull the line low at a time.

    On the Raspberry Pi board, there are 1.8kΩ pull-up resistorsto 3.3V, so external ones are not normally required.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Bidirectional Communication on a Single Line

    How can multiple devices communicate on the same line?Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.

    Any device which is not speaking must let their output float.Only one device can pull the line low at a time.

    On the Raspberry Pi board, there are 1.8kΩ pull-up resistorsto 3.3V, so external ones are not normally required.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Bidirectional Communication on a Single Line

    How can multiple devices communicate on the same line?Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.Any device which is not speaking must let their output float.

    Only one device can pull the line low at a time.

    On the Raspberry Pi board, there are 1.8kΩ pull-up resistorsto 3.3V, so external ones are not normally required.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Bidirectional Communication on a Single Line

    How can multiple devices communicate on the same line?Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.Any device which is not speaking must let their output float.Only one device can pull the line low at a time.

    On the Raspberry Pi board, there are 1.8kΩ pull-up resistorsto 3.3V, so external ones are not normally required.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Bidirectional Communication on a Single Line

    How can multiple devices communicate on the same line?Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.Any device which is not speaking must let their output float.Only one device can pull the line low at a time.

    On the Raspberry Pi board, there are 1.8kΩ pull-up resistorsto 3.3V, so external ones are not normally required.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Mixing devices with different supply voltages

    Do you need optoisolators if devices require different supplyvoltages?

    Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.If the pull-up resistors only go to the lowest supply voltage,then the data line will never be too high.Since the Raspberry Pi has 1.8kΩ pull-up resistors to 3.3V,connecting to an I2C device with a 5V supply will not cause aproblem for the Pi.Note, however, if 3.3V inputs are not high enough for thedevice, then it might not operate correctly.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Mixing devices with different supply voltages

    Do you need optoisolators if devices require different supplyvoltages?

    Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.If the pull-up resistors only go to the lowest supply voltage,then the data line will never be too high.Since the Raspberry Pi has 1.8kΩ pull-up resistors to 3.3V,connecting to an I2C device with a 5V supply will not cause aproblem for the Pi.Note, however, if 3.3V inputs are not high enough for thedevice, then it might not operate correctly.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Mixing devices with different supply voltages

    Do you need optoisolators if devices require different supplyvoltages?

    Each device has an open-collector (or open-drain) output.

    A pull-up resistor on the line allows it to go high whennothing is pulling it low.If the pull-up resistors only go to the lowest supply voltage,then the data line will never be too high.Since the Raspberry Pi has 1.8kΩ pull-up resistors to 3.3V,connecting to an I2C device with a 5V supply will not cause aproblem for the Pi.Note, however, if 3.3V inputs are not high enough for thedevice, then it might not operate correctly.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Mixing devices with different supply voltages

    Do you need optoisolators if devices require different supplyvoltages?

    Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.

    If the pull-up resistors only go to the lowest supply voltage,then the data line will never be too high.Since the Raspberry Pi has 1.8kΩ pull-up resistors to 3.3V,connecting to an I2C device with a 5V supply will not cause aproblem for the Pi.Note, however, if 3.3V inputs are not high enough for thedevice, then it might not operate correctly.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Mixing devices with different supply voltages

    Do you need optoisolators if devices require different supplyvoltages?

    Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.If the pull-up resistors only go to the lowest supply voltage,then the data line will never be too high.

    Since the Raspberry Pi has 1.8kΩ pull-up resistors to 3.3V,connecting to an I2C device with a 5V supply will not cause aproblem for the Pi.Note, however, if 3.3V inputs are not high enough for thedevice, then it might not operate correctly.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Mixing devices with different supply voltages

    Do you need optoisolators if devices require different supplyvoltages?

    Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.If the pull-up resistors only go to the lowest supply voltage,then the data line will never be too high.Since the Raspberry Pi has 1.8kΩ pull-up resistors to 3.3V,connecting to an I2C device with a 5V supply will not cause aproblem for the Pi.

    Note, however, if 3.3V inputs are not high enough for thedevice, then it might not operate correctly.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Mixing devices with different supply voltages

    Do you need optoisolators if devices require different supplyvoltages?

    Each device has an open-collector (or open-drain) output.A pull-up resistor on the line allows it to go high whennothing is pulling it low.If the pull-up resistors only go to the lowest supply voltage,then the data line will never be too high.Since the Raspberry Pi has 1.8kΩ pull-up resistors to 3.3V,connecting to an I2C device with a 5V supply will not cause aproblem for the Pi.Note, however, if 3.3V inputs are not high enough for thedevice, then it might not operate correctly.

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    SDA

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    SDA

    All floating; SDA is HIGH.Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    SDA

    One output LOW; SDA is LOW.Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    SDA

    SCL

    START

    MSB LSB ACK

    STOP

    I2C ; bits are read when SCL is HIGHACK is sent by receiver if OKsender must release SDA after LSB

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    SDA

    SCL

    START

    MSB LSB NACK

    STOP

    I2C ; bits are read when SCL is HIGHNACK is sent by master-receiver if OKsender must release SDA after LSB

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    I2C write to slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START W

    Slave address

    I2C write to slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA

    Slave address

    I2C write to slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA

    Slave addressRegister number

    I2C write to slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA A

    Slave addressRegister number

    I2C write to slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA A

    Slave addressRegister number

    Register data

    I2C write to slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA A A

    Slave addressRegister number

    Register data

    I2C write to slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START STOPWA A

    ASlave address

    Register numberRegister data

    I2C write to slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    I2C read from slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START W

    Slave address

    I2C read from slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA

    Slave address

    I2C read from slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA

    Slave addressRegister number

    I2C read from slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA A

    Slave addressRegister number

    I2C read from slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA A R

    Slave addressRegister number

    Slave address

    I2C read from slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA A RA

    Slave addressRegister number

    Slave address

    I2C read from slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA A RA

    Slave addressRegister number

    Slave addressRegister data

    I2C read from slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START WA A RA N

    Slave addressRegister number

    Slave addressRegister data

    I2C read from slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    START STOPWA A RA

    NSlave addressRegister number

    Slave addressRegister data

    I2C read from slave register

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Smbus

    bus = smbus.SMBus(1)create objectbus.read i2c block data(addr,cmd)read from devicebus.write i2c block data(addr,cmd,vals)write to device

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Smbus

    bus = smbus.SMBus(1)create object

    bus.read i2c block data(addr,cmd)read from devicebus.write i2c block data(addr,cmd,vals)write to device

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Smbus

    bus = smbus.SMBus(1)create objectbus.read i2c block data(addr,cmd)read from device

    bus.write i2c block data(addr,cmd,vals)write to device

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Smbus

    bus = smbus.SMBus(1)create objectbus.read i2c block data(addr,cmd)read from devicebus.write i2c block data(addr,cmd,vals)write to device

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Smbus

    #!/ u s r / b i n / pythoni m p o r t smbusbus = smbus . SMBus ( 1 )DEVICE ADDRESS = 0 x15DEVICE REG MODE1 = 0 x00DEVICE REG LEDOUT0 = 0 x1d#Write a s i n g l e r e g i s t e rbus . w r i t e b y t e d a t a (DEVICE ADDRESS ,

    DEVICE REG MODE1 , 0 x80 )

    #Write an a r r a y o f r e g i s t e r sl e d o u t v a l u e s = [ 0 x f f , 0 x f f ,

    0 x f f , 0 x f f , 0 x f f , 0 x f f ]bus . w r i t e i 2 c b l o c k d a t a (DEVICE ADDRESS ,

    DEVICE REG LEDOUT0 , l e d o u t v a l u e s )

    Terry Sturtevant Electronics Serial Communication-I2C

  • Serial Communication -I2CRaspberry Pi Python I2C Library

    Smbus#!/ u s r / b i n / pythoni m p o r t smbusbus = smbus . SMBus ( 1 )DEVICE ADDRESS = 0 x15DEVICE REG MODE1 = 0 x00DEVICE REG LEDOUT0 = 0 x1d#Write a s i n g l e r e g i s t e rbus . w r i t e b y t e d a t a (DEVICE ADDRESS ,

    DEVICE REG MODE1 , 0 x80 )

    #Write an a r r a y o f r e g i s t e r sl e d o u t v a l u e s = [ 0 x f f , 0 x f f ,

    0 x f f , 0 x f f , 0 x f f , 0 x f f ]bus . w r i t e i 2 c b l o c k d a t a (DEVICE ADDRESS ,

    DEVICE REG LEDOUT0 , l e d o u t v a l u e s )

    Terry Sturtevant Electronics Serial Communication-I2C

    Serial Communication -I2CRaspberry Pi Python I2C Library