I have a Teensy 4.1 and a RaspberryPi connected via I2C with a working connection. I reproduced the results from this tutorial: (https://dronebotworkshop.com/i2c-arduino-raspberry-pi/) (Code also below), where you get the teensy onboard LED to flash with a python program using smbus2 on the raspberry.
I can flash the LED as intended, but I noticed that the I2C bus is full of messages when I print them to the serial monitor. When I use Seriell.println() in the callback function to see arriving bytes, my terminal is flooded with the one byte I tried to send, even though there is only a single call from the master to write to the bus. I get around 3500 calls/second to the ReceiveEvent() callback function which indicates that a I2C message was received.
When I print out the number of bytes in a I2C message using Wire.available(), it is always capped at the maximum of 40 bytes. This should be only 1 byte, because the master only calls bus.write_byte() once.
This continues even if I shutdown Ubuntu on the raspberry, but ends when I pull its power plug.
Question: Why are there so many bytes sent over the I2C bus? As I currently understand there should only be a single byte sent, or am I missunderstanding something?
EDIT: I used a differerent Teensy (4.1) and the same code worked as expected, not flooding the I2C connection... So problem successfully defered!
Teensy/Arduino Code (I2C Slave):
// Include the Wire library for I2C
#include <Wire.h>
volatile int i = 0;
// Function that executes whenever data is received from master
void receiveEvent(int howMany) {
while (Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
digitalWrite(LED_BUILTIN, c);
}
i++;
}
void setup() {
// Join I2C bus as slave with address 8
Wire.begin(0x8);
// Call receiveEvent when data received
Wire.onReceive(receiveEvent);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
int j = i;
delay(2000);
Serial.print(i);
Serial.print(" +");
Serial.println(i-j);
}
Raspberry Python Code (I2C Master):
from smbus import SMBus
addr = 0x8 # bus address
bus = SMBus(1) # indicates /dev/ic2-1
numb = 1
print ("Enter 1 for ON or 0 for OFF")
while numb == 1:
ledstate = input(">>>> ")
if ledstate == "1":
bus.write_byte(addr, 0x1) # switch it on
elif ledstate == "0":
bus.write_byte(addr, 0x0) # switch it on
else:
numb = 0
>>>>
once or more than once? Do you have pull-up resistors on SDA and SCL?>>>>
in python? No, just once.