Hi so for an assignment I need to connect the master (UNO) to slave (Nano) using I2C bus. Then I need to connect I2C LCD with the Slave(NANO) to show the data sent from Master(UNO). Since I have already used the A4, A5 (SDA & SCL) pins to create serial data communication between master and slave, how do I connect my I2C_LCD with the NANO?
Connect as follows (Fig-1):
Figure-1:
I2CLCD NANO
SCL SCL/A5
SDA SDA/A4
5V 5V
GND GND
I2C is a bus; you can connect (theoretically) as many as 119 devices/sensors.
The attached file may worth reading and practicing.
Ch-7I2CLec.pdf (334.4 KB)
Assalamualaikum Sir, Thank you for replying.
I have tried doing this but then I am getting wrong value from the Master. I was supposed to receive '5' but receiving '255' after adding I2C_LCD.
Is there any way to create a new I2C bus?
No need to create a new I2C Bus. The present bus should work.
Please, post your sketches for both Master and Slave.
Sketch for Master
#include<Wire.h>
#define slaveAddr 0b0010011
byte m;
void setup()
{
Serial.begin(9600);
Wire.begin();
pinMode(6, OUTPUT);
}
void loop()
{
Serial.println("Enter m");
m = Serial.parseInt();
Serial.println(m);
Wire.beginTransmission(slaveAddr);
Wire.write(m);
Wire.requestFrom(slaveAddr, 1);
byte n=Wire.read();
Serial.print("Slave sent = ");
Serial.println(n);
Wire.endTransmission();
if (m>n)
{
for (int i =1; i<=n; i++)
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
delay(1000);
}
delay(1000);
}
Sketch for Slave :
#include<Wire.h>
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define slaveAddr 0b0010011
byte y, n;
volatile bool flag=false;
void setup()
{
Serial.begin(74880);
Wire.begin(slaveAddr);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
lcd.init();
lcd.backlight();
}
void loop()
{
Serial.print("Enter n");
n = Serial.parseInt();
Serial.println(n);
if (flag==true)
{
Serial.print("Master Sent =");
Serial.println(y, HEX);
flag=false;
}
Wire.requestFrom(slaveAddr, 1);
delay(1000);
lcd.setCursor(0, 0);
if (n>y)
{
lcd.print(y);
}
}
void receiveEvent()
{
y=Wire.read();
flag=true;
}
void requestEvent()
{
Wire.write(n);
}
Add Wire.endTransmission() to execute the transmission before reading from the device.
If you have written this sketch yourself meaning you know I2C Bus programming and correct/moderate it; else, begin with the execution of the following steps:
1. Connect only UNO and NANO with external pull-up resistors as shown in Fig-1 of post #2.
2. Check that you can exchange data between UNO and NANO by uploading the following sketches: Master sends 0x23 to Slave and Slave sends 0x56 to master at 1-sec interval. Check the respective Serial Monitor shows data correctly at 1-sec interval.
Master Sketch:
#include<Wire.h>
#define slaveAddress 0b0010011
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop()
{
Wire.beginTransmission(slaveAddress);
Wire.write(0x23) ; //sending data byte 23 hex
Wire.endTransmission();
//------------------------------------
Wire.requestFrom(slaveAddress, 1); //requesting 1-byte data from Slave
byte y = Wire.read();
Serial.println(y, HEX); //shows: 56 hex
//-----------------------------
delay(1000); //test interval
}
Slave Sketch:
#include<Wire.h>
#define slaveAddress 0b0010011
byte y;
volatile bool flag = false;
void setup()
{
Serial.begin(9600);
Wire.begin(slaveAddress);
Wire.onReceive(receiveEvent);
Wire.onRequest(sendEvent);
}
void loop()
{
if(flag == true)
{
Serial.println(y, HEX);
flag = false;
}
}
void receiveEvent(int howMany)
{
y = Wire.read();
flag = true;
}
void sendEvent()
{
Wire.write(0x56); //sending 1-byte to Master
}
3. Connect I2CLCD with I2C Bus as per Fig- 1 of post #2.
4. Upload the following sketch to find the address of I2CLCD.
#include<Wire.h>
byte busStatus;
void setup()
{
Serial.begin(9600);
Wire.begin();
for (int i2cAddress = 0x00; i2cAddress < 0x80; i2cAddress++)
{
Wire.beginTransmission(i2cAddress);
busStatus = Wire.endTransmission();
if (busStatus == 0x00)
{
Serial.print("I2C Device found at address: 0x");
Serial.println(i2cAddress, HEX);
}
else
{
Serial.print("I2C Device not found at address: 0x");
Serial.println(i2cAddress, HEX);
}
}
}
void loop()
{
}
5. Upload the following sketch to do the functional check of I2CLCD.
#include<LiquidCrystal_I2C.h>
#include<Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
Serial.begin(9600);
Wire.begin();
lcd.begin();//or lcd.init();
lcd.backlight();
lcd.setCursor(0, 0); //DPos (0-15), LPos (0-1)
//----------------------
Wire.beginTransmission(0x27);
byte busStatus = Wire.endTransmission();
if (busStatus != 0)
{
Serial.println("I2CLCD is not found!");
while (1); //wait for ever
}
Serial.print("I2CLCD is found!");
//---------------------
lcd.print("Forum"); //show message
}
void loop() {}
Thank you sir this works
Which "this"?
Always, refer to post # number.
Is it really needed?
@DrDiettrich
Should work as RESTART followed by Wire.endTransmission() which is there. The following sketch works well.
#include<Wire.h>
#define slaveAddress 0b0010011
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop()
{
Wire.beginTransmission(slaveAddress);
Wire.write(0x23) ; //sending data byte 23 hex
//Wire.endTransmission();
//------------------------------------
Wire.requestFrom(slaveAddress, 1); //requesting 1-byte data from Slave
byte y = Wire.read();
Serial.println(y, HEX); //shows: 56 hex
Wire.endTransmission();
//-----------------------------
delay(1000); //test interval
}
Yes. endTransmission() does the entire transmission.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
