[Facts] Raspberry Pi + I²C + MCP23017: digital output toggle timespan (Bash, Python, C)

posted in: computer, electronics | 0

purpose:

measuring the timespan between rising/falling-edge (=off/on/off-change) of a digital-output-bank (GPA0…GPA7)

testing equipment:

-Raspberry Pi 3 Modell B
-I2C-adress of MCP23017-chip = 0x20
-MCP23017 register-adress of 8 digital-outputs: 0x14
-Owon SDS7102 digital ocsilloscope


conclusion:

I²C-baudratebashPythonC
100.0005,18 ms @ 75 kHz SCL0,51 ms @ 62,5 kHz SCL0,65 ms Hz @ 62,5 kHz SCL
400.0004,85 ms @ 300 kHz SCL0,13 ms @ 300 kHz SCL0,155 ms @ 300 kHz SCL
1.700.000no toggling @ 1.064 kHz SCL
  • baudrate=100.000: clockspeed varies between different codes
  • measured clockspeed was always lower than the set baudrate
  • the higher the baudrate, the less the on-off-timespan
  • baudrate=1.7M: the digital outputs aren’t able to toogle so fast

code examples:

manipulate I²C-baudrate: (e.g. to baudrate=400.000)

  1. edit: /boot/config.txt
  2. change: dtparam=i2c_arm=on,i2c_arm_baudrate=400000
  3. reboot (without rebooting it won’t work)

Bash: the following packages must be installed: i2c-dev

#!/bin/bash
i2cset -y 1 0x20 0x00 0x00;

while true;
do
  i2cset -y 1 0x20 0x14 0xff;
  i2cset -y 1 0x20 0x14 0x00;
done

Python: the following packages must be installed: python-smbus

#!/usr/bin/python
import smbus

mcp23017 = smbus.SMBus(1)
mcp23017.write_byte_data(0x20,0x00,0x00)

while True:
  mcp23017.write_byte_data (0x20,0x14,0xff)
  mcp23017.write_byte_data (0x20,0x14,0x00)

C: the following packages must be installed: i2c-dev

#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>

int main(){
  int file = open("/dev/i2c-1", I2C_RDWR);
  ioctl(file, I2C_SLAVE, 0x20);
  i2c_smbus_write_word_data(file, 0x00, 0x00);

  for (;;) {
    i2c_smbus_write_word_data(file, 0x14, 0xFF);
    i2c_smbus_write_word_data(file, 0x14, 0x00);
  }
}