I’m trying to configure a Photon and attiny85 using I2C using the Arduino I2C scanner recommended in the Particle I2C tutorial and have checked both my code and wiring, but I keep getting a “No devices found” message in my serial monitor.
Here is my code for my photon:
#include "application.h"
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial);
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
And here is my code for my ATtiny85:
#include "TinyWireS.h" // wrapper class for I2C slave routines
#define I2C_SLAVE_ADDR 0x26 // i2c slave address
void setup(){
TinyWireS.begin(I2C_SLAVE_ADDR); // init I2C Slave mode
}
void loop(){
byte byteRcvd = 0;
if (TinyWireS.available()){ // got I2C input!
TinyWireS.send(5); // send 5 back to master
}
}
I’m pretty new to electronics and this kind of programming, so any help would be GREATLY appreciated-- thank you!
Thanks so much for your help, @rickkas7. I had the pull-up resistors in the wrong spot, and now have them in place according to this diagram, but now when I run the scanner I get a bunch of addresses being detected, none of which are the actual address on the device.
Here is the serial output I’m getting (with multiple addresses):
Scanning...
I2C device found at address 0x05 !
I2C device found at address 0x0F !
I2C device found at address 0x2C !
I2C device found at address 0x2E !
I2C device found at address 0x38 !
I2C device found at address 0x3E !
I2C device found at address 0x68 !
done
Any thoughts on why this might be happening? I tried changing the slave address, but I still got multiple addresses found (although they were different addresses than those listed above). Again, thanks for your help.
I got it working, but am not really sure how. For those wondering, here is my working code.
Photon:
#include "application.h"
void setup()
{
Wire.begin();
Serial.begin(9600);
delay(10000);
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
ATtiny85 code:
#include "TinyWireS.h" // wrapper class for I2C slave routines
#define I2C_SLAVE_ADDR 0x27 // i2c slave address
void setup(){
TinyWireS.begin(I2C_SLAVE_ADDR); // init I2C Slave mode
}
void loop(){
if (TinyWireS.available()){ // got I2C input!
TinyWireS.send(5); // send 5 back to master
}
}
@gstill2 Good to hear you got it working. I did a bit of reading up on the TinyWireS library as this is something I have considered using to allow me to communicate between the Photon and slave MCUs.
Where did you get the TinyWireS library from and which version is it? I assume you have imported this as a .zip into the Arduino IDE and then used an ISP to program your ATtiny85?
There are some issues if I understand in the use of this library for I2C and in fact any two-wire library on the ATtiny family of MCUs due to them not having hardware support for I2C and extremely limited resources. The examples I saw for the ATtiny code used TinyWireS on receive callback rather than continually checking in the loop. TinyWireS.onReceive( onI2CReceive ); I haven’t tested this, it could be that it is hit and miss because the loop is missing signals from the photon master?
hey @gstill2, your question really inspired me. I wanted to do a project like this about a year ago but I gave up because it was too hard. I am pleased to say I had a lot more success this time and I wanted to share what I found. It uses the code and links you posted so it’s loosely related to what you needed, but I mainly just wanted to say thanks and pay it forward. It was a nightmare to put this project together (also new to this) and my hope is that we can make these projects easier for everyone in the future .
The project involves taking light readings from a photo resistor, convert it to a byte, send it to the photon on request over i2c, and publish the result to particle’s device cloud.
I made a repo with:
A cleaned up library for adding TinyWireSlave to the Arduino IDE