| 1 | # I2C Communication interface |
| 2 | |
| 3 | The I2C interface consists of 2 pins: |
| 4 | |
| 5 | + SCL, also known as the clock pin. PC0 on Atmega32 |
| 6 | + SDA, also known as the data pin. PC1 on Atmega32 |
| 7 | |
| 8 | With these two connections, it is possible to get data from a variety of sensors measuring |
| 9 | physical parameters. Visit the [sensors page](../../sensors) for details on using them via the GUI. |
| 10 | |
| 11 | |
| 12 | ## I2C function calls |
| 13 | |
| 14 | === "I2CScan" |
| 15 | ```python hl_lines="1" |
| 16 | def I2CScan() |
| 17 | scan the I2C bus, and return a list of addresses that responded |
| 18 | |
| 19 | return: list of numbers between 0-127. |
| 20 | |
| 21 | ``` |
| 22 | |
| 23 | === "I2CWriteBulk" |
| 24 | ```python hl_lines="1" |
| 25 | def I2CWriteBulk(address,bytestream): |
| 26 | write a set of bytes to an I2C address |
| 27 | |
| 28 | address: Address of I2C slave device. 0-127 |
| 29 | bytestream: list of bytes to write |
| 30 | return: True if success. |
| 31 | |
| 32 | ``` |
| 33 | |
| 34 | === "I2CReadBulk" |
| 35 | ```python hl_lines="1" |
| 36 | def I2CReadBulk(address,register,total_bytes): |
| 37 | write a set of bytes to an I2C address |
| 38 | |
| 39 | address: Address of I2C slave device. 0-127 |
| 40 | register: The starting address in the I2C slave device from where bytes are to be read |
| 41 | total_bytes: Total number of bytes to read |
| 42 | return: bytes, timeout |
| 43 | "ignore contents if timeout==True" |
| 44 | ``` |
| 45 | |
| 46 | === "MPU6050 example" |
| 47 | ```python hl_lines="1" |
| 48 | # read values from MPU6050 accelerometer+gyro with address 0x68. refer datasheet. |
| 49 | from kuttyPy import * |
| 50 | I2CWriteBulk(0x68,[0x1B,0<<3]) #250-> Gyro Range . 250,500,1000,2000 -> 0,1,2,3 -> shift left by 3 positions |
| 51 | I2CWriteBulk(0x68,[0x1C,0<<3]) #2-> Accelerometer Range. 2,4,8,16 -> 0,1,2,3 -> shift left by 3 positions |
| 52 | I2CWriteBulk(0x68,[0x6B, 0x00]) #poweron |
| 53 | |
| 54 | bytevalues,timeout = self.I2CReadBulk(0x68, 0x3B ,14) |
| 55 | if not timeout: |
| 56 | values = [(bytevalues[x*2+1]<<8)|b[x*2] for x in range(7)] #Ax,Ay,Az,temp,Gx,Gy,Gz are 16-bit from 2 b-bit each. |
| 57 | print('Ax = %d, Ay = %d, Az = %d'%(values[0],values[1],values[2])) |
| 58 | print('Gx = %d, Gy = %d, Gz = %d'%(values[4],values[5],values[6])) |
| 59 | |
| 60 | ``` |
| 61 | |
| 62 | |
| 63 | ## Pre-Supported I2C Sensors |
| 64 | |
| 65 | - MPU6050 3 Axis Accelerometer, 3 axis Angular velocity (Gyro) |
| 66 | - MPU9250 9-DOF sensor |
| 67 | - MS5611 : 24 bit pressure and temperature sensor. Can resolve 15cm height variations |
| 68 | - TSL2561 Luminosity measurements |
| 69 | - BMP280 Pressure and Temperature sensor |
| 70 | - MCP4725 Single channel DAC |
| 71 | - PCA9685 PWM controller |
| 72 | - MLX90614 Passive IR |
| 73 | |
| 74 | |
| 75 | ### MPU6050 accelerometer + gyroscope |
| 76 | The MPU6050 is a Micro Electro-Mechanical Systems (MEMS) which consists of a 3-axis Accelerometer |
| 77 | and 3-axis Gyroscope inside it. It can measure acceleration, velocity, orientation, |
| 78 | and many other motion related parameters. Commonly used in drones for stability feedback, |
| 79 | it also has a lot of potential applications in Physics Experiments. |
| 80 | |
| 81 | ```python hl_lines="1" |
| 82 | def MPU6050_init() |
| 83 | initialize the MPU6050 sensor(address=0x68), and set ranges to max sensitivity. |
| 84 | ``` |
| 85 | |
| 86 | ```python hl_lines="1" |
| 87 | def MPU6050_gyro_range(range) |
| 88 | set the sensitivity of the gyroscope |
| 89 | range: 0,1,2 or 3 corresponding to +-250,+-500,+-1000,+-2000 |
| 90 | ``` |
| 91 | |
| 92 | ```python hl_lines="1" |
| 93 | def MPU6050_accel_range(range) |
| 94 | set the sensitivity of the accelerometer |
| 95 | range: 0,1,2 or 3 corresponding to +-2,+-4,+-8,+-16 |
| 96 | ``` |
| 97 | |
| 98 | |
| 99 | ```python hl_lines="1" |
| 100 | def MPU6050_kalman_set(value) |
| 101 | Enable a moving average to reduce noise level of the sensor. |
| 102 | Useful to reject high frequency oscillations. |
| 103 | fetches 50 values, and activates a Kalman filter. To ignore an activated Kalman Filter, an optional False value can be supplied to `MPU6050_all` |
| 104 | |
| 105 | value: kalman averaging coefficient. Float. |
| 106 | ``` |
| 107 | |
| 108 | |
| 109 | ```python hl_lines="1" |
| 110 | def MPU6050_all() |
| 111 | fetch values from the sensor |
| 112 | return: list of 7 16-bit integers |
| 113 | ``` |
| 114 | |
| 115 | Example with MPU6050 |
| 116 | |
| 117 | ```python |
| 118 | from kuttyPy import * |
| 119 | MPU6050_init() #Initialize the sensor |
| 120 | x = MPU6050_all() #Fetch readings |
| 121 | if x is not None: |
| 122 | print('Ax = %d, Ay = %d, Az = %d'%(x[0],x[1],x[2])) |
| 123 | print('Gx = %d, Gy = %d, Gz = %d'%(x[4],x[5],x[6])) |
| 124 | |
| 125 | ``` |
| 126 | |
| 127 | ### TSL2561 Luminosity sensor |
| 128 | The TSL2561 is a light sensor which has a |
| 129 | linear response across most of the visible spectrum. It has two photodiodes |
| 130 | with corrresponding ADCs, and a very wide range of operation. Ideal for optics experiments |
| 131 | |
| 132 | |
| 133 | ```python hl_lines="1" |
| 134 | def TSL2561_init() |
| 135 | initialize the TSL2561 sensor(address=0x39). |
| 136 | ``` |
| 137 | |
| 138 | ```python hl_lines="1" |
| 139 | def TSL2561_gain(gain) |
| 140 | set the gain |
| 141 | gain: 0 => 1x , 1 => 16x |
| 142 | ``` |
| 143 | |
| 144 | ```python hl_lines="1" |
| 145 | def TSL2561_timing(timing) |
| 146 | set the integration time : 3mS , 101mS, 403mS |
| 147 | timing: 0 => 3mS , 1 => 101mS, 2 => 403mS |
| 148 | ``` |
| 149 | |
| 150 | ```python hl_lines="1" |
| 151 | def TSL2561_all() |
| 152 | fetch values from the sensor |
| 153 | return: list of 3 integers. Total light, IR luminous intensity |
| 154 | ``` |
| 155 | |
| 156 | Example with TSL2561 |
| 157 | |
| 158 | ```python |
| 159 | from kuttyPy import * |
| 160 | TSL2561_init() #Initialize the sensor |
| 161 | x = TSL2561_all() #Fetch readings |
| 162 | if x is not None: |
| 163 | print('Total Light = %d, Infrared = %d'%(x[0],x[1])) |
| 164 | |
| 165 | ``` |
| 166 | |
| 167 | |
| 168 | ### BMP280 Pressure and temperature sensor |
| 169 | BMP280 is an absolute barometric pressure sensor developed by Bosch Sensortec. |
| 170 | The sensor module is housed in an extremely compact package, and is very handy to |
| 171 | demonstrate physics concepts to high school students. |
| 172 | |
| 173 | === "BMP280_init" |
| 174 | ```python hl_lines="1" |
| 175 | def BMP280_init() |
| 176 | initialize the BMP280 sensor(address=118). |
| 177 | ``` |
| 178 | |
| 179 | === "BMP280_all" |
| 180 | ```python hl_lines="1" |
| 181 | def BMP280_all() |
| 182 | fetch values from the sensor |
| 183 | return: list of 2 integers. Pressure(mBar), Temperature(C) |
| 184 | ``` |
| 185 | |
| 186 | === "Example" |
| 187 | ```python |
| 188 | from kuttyPy import * |
| 189 | BMP280_init() #Initialize the sensor |
| 190 | x = BMP280_all() #Fetch readings |
| 191 | if x is not None: |
| 192 | print('Pressure = %.2f, Temperature = %.2f'%(x[0],x[1])) |
| 193 | ``` |
| 194 | |
| 195 | |
| 196 | ### MS5611 Pressure and temperature sensor |
| 197 | MS5611 is a high resolution altimeter developed by MEAS. |
| 198 | It has better resolution than the BMP280, and about 15cm height difference can be resolved. |
| 199 | |
| 200 | === "MS5611_init" |
| 201 | ```python hl_lines="1" |
| 202 | def MS5611_init() |
| 203 | initialize the MS5611 sensor(address=119). |
| 204 | ``` |
| 205 | |
| 206 | === "MS5611_all" |
| 207 | ```python hl_lines="1" |
| 208 | def MS5611_all() |
| 209 | fetch values from the sensor |
| 210 | return: list of 2 integers. Pressure(mBar), Temperature(C) |
| 211 | ``` |
| 212 | |
| 213 | === "Example" |
| 214 | ```python |
| 215 | from kuttyPy import * |
| 216 | MS5611_init() #Initialize the sensor |
| 217 | x = MS5611_all() #Fetch readings |
| 218 | if x is not None: |
| 219 | print('Pressure = %.2f, Temperature = %.2f'%(x[0],x[1])) |
| 220 | ``` |
| 221 | |
| 222 | ### TCS34725 RGB color sensor |
| 223 | |
| 224 | === "TCS34725_init" |
| 225 | ```python hl_lines="1" |
| 226 | def TCS34725_init() |
| 227 | initialize the TCS34725 sensor(address=41). |
| 228 | ``` |
| 229 | |
| 230 | === "TCS34725_all" |
| 231 | ```python hl_lines="1" |
| 232 | def TCS34725_all() |
| 233 | fetch values from the sensor |
| 234 | return: list of 3 integers. [Red, Green, Blue] |
| 235 | ``` |
| 236 | |
| 237 | === "TCS34725_gain" |
| 238 | ```python hl_lines="1" |
| 239 | def TCS34725_gain(gain) |
| 240 | set the gain of the sensor |
| 241 | gain: 0,1,2,3 => 1x,4x,16x,60x |
| 242 | ``` |
| 243 | |
| 244 | === "Example" |
| 245 | ```python |
| 246 | from kuttyPy import * |
| 247 | TCS34725_init() #Initialize the sensor |
| 248 | x = TCS34725_all() #Fetch readings |
| 249 | if x is not None: |
| 250 | print(x) |
| 251 | ``` |
| 252 | |
| 253 | |
| 254 | ### MLX90614 passive IR |
| 255 | The MLX90614 is an infrared thermometer for non-contact temperature measurements made by Melexis |
| 256 | Both the IR sensitive thermopile detector chip and the signal conditioning ASIC are |
| 257 | integrated in the same TO-39 can. |
| 258 | |
| 259 | === "MLX90614_init" |
| 260 | ```python hl_lines="1" |
| 261 | def MLX90614_init() |
| 262 | initialize the sensor(address=0x5A). |
| 263 | ``` |
| 264 | |
| 265 | === "MLX90614_all" |
| 266 | ```python hl_lines="1" |
| 267 | def MLX90614_all() |
| 268 | fetch values from the sensor |
| 269 | return: [Temperature] in degree celcius |
| 270 | ``` |
| 271 | |
| 272 | === "Example" |
| 273 | ```python |
| 274 | from kuttyPy import * |
| 275 | MLX90614_init() #Initialize the sensor |
| 276 | x = MLX90614_all() #Fetch readings |
| 277 | if x is not None: |
| 278 | print(x[0]) |
| 279 | ``` |
| 280 | |
| 281 | |
| 282 | ## Output devices with I2C |
| 283 | |
| 284 | ### MCP4725 DAC |
| 285 | MCP4725 is a single-channel 12-bit Digital to Analog converter from Microchip Technology. |
| 286 | |
| 287 | === "MCP4725_init" |
| 288 | ```python hl_lines="1" |
| 289 | def MCP4725_init() |
| 290 | initialize the MCP4725 DAC(address=0x60). |
| 291 | ``` |
| 292 | |
| 293 | === "MCP4725_set" |
| 294 | ```python hl_lines="1" |
| 295 | def MCP4725_all(value) |
| 296 | write a 12 bit number to the DAC |
| 297 | value: 0 - 4095 |
| 298 | ``` |
| 299 | |
| 300 | === "Example" |
| 301 | ```python |
| 302 | from kuttyPy import * |
| 303 | MCP4725_init() #Initialize the DAC |
| 304 | MCP4725_set(1024) #Write |
| 305 | ``` |
| 306 | |
| 307 | ### PCA9685 PWM generator |
| 308 | PCA9685 is a 16-channel PWM generator. It can be used to adjust brightness of LEDs via high frequency output, |
| 309 | or for controlling servo motors in a low-frequency mode. The emphasis is currently on driving SG-90 servos |
| 310 | |
| 311 | === "PCA9685_init" |
| 312 | ```python hl_lines="1" |
| 313 | def PCA9685_init() |
| 314 | initialize the PCA9685 (address = 64). |
| 315 | ``` |
| 316 | |
| 317 | === "PCA9685_set" |
| 318 | ```python hl_lines="1" |
| 319 | def PCA9685_set(channel, angle) |
| 320 | channel: 1-16 . Channel number for configuring |
| 321 | angle: 0 - 180 |
| 322 | ``` |
| 323 | |
| 324 | "Example" |
| 325 | ```python |
| 326 | from kuttyPy import * |
| 327 | PCA9685_init() #Initialize the DAC |
| 328 | PCA9685_set(1,90) #Servo using PWM signal from channel 1 will be set to 90 degrees |
| 329 | PCA9685_set(2,90) #Servo using PWM signal from channel 2 will be set to 90 degrees |
| 330 | ``` |
| 331 |