I2C Led blink blink

Hi everyone, hope all is well!

I got interested in the whole I2C way of doing projects.

Can you please guide me to a simple I2C project where the master blinks an LED on the slave?

I have found documentation about how to make an I2C setup on a master and a slave.

I just need to know “Where” to insert the LED blink sketch in the slave’s code.

Thank you

Please post the code used in the slave. Autoformat the text in the IDE and use code tags when pasting it.

@Mars-Sojourner, your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that you write ( :wink: ), not for questions.

Please provide details; we don't know what you've found.

Thank you for your reply. Here is the code.

// Arduino MASTER sketch

#include <Wire.h>

byte i2c_rcv;               // data received from I2C bus
unsigned long time_start;   // start time in milliseconds
int stat_LED;               // status of LED: 1 = ON, 0 = OFF
byte value_pot;             // potentiometer position

void setup() {
  Wire.begin(); // join I2C bus as the master

  // initialize global variables
  i2c_rcv = 255;
  time_start = millis();
  stat_LED = 0;

  pinMode(13, OUTPUT);    // set pin 13 as an output
}

void loop() {
  // read potentiometer position
  value_pot = analogRead(A0);   // read the voltage at pin A0 (potentiometer voltage)

  // send potentiometer position to Slave device 0x08
  Wire.beginTransmission(0x08); // informs the bus that we will be sending data to slave device 8 (0x08)
  Wire.write(value_pot);        // send value_pot
  Wire.endTransmission();       // informs the bus and the slave device that we have finished sending data

  Wire.requestFrom(0x08, 1);    // request potentiometer position from slave 0x08
  if (Wire.available()) {       // read response from slave 0x08
    i2c_rcv = Wire.read();
  }

  // blink logic code
  if ((millis() - time_start) > (1000 * (float)(i2c_rcv / 255))) {
    stat_LED = !stat_LED;
    time_start = millis();
  }
  digitalWrite(13, stat_LED);
}
// Arduino SLAVE sketch

#include <Wire.h>

byte i2c_rcv;               // data received from I2C bus
unsigned long time_start;   // start time in mSec
int stat_LED;               // status of LED: 1 = ON, 0 = OFF
byte value_pot;             // potentiometer position

void setup() {
  Wire.begin(0x08);           // join I2C bus as Slave with address 0x08

  // event handler initializations
  Wire.onReceive(dataRcv);    // register an event handler for received data
  Wire.onRequest(dataRqst);   // register an event handler for data requests

  // initialize global variables
  i2c_rcv = 255;
  time_start = millis();
  stat_LED = 0;

  pinMode(13, OUTPUT);        // set pin 13 mode to output
}

void loop() {

  value_pot = analogRead(A0); // read analog value at pin A0 (potentiometer voltage)

  // blink logic code
  if ((millis() - time_start) > (1000 * (float)(i2c_rcv / 255))) {
    stat_LED = !stat_LED;
    time_start = millis();
  }
  digitalWrite(13, stat_LED);
}

//received data handler function
void dataRcv(int numBytes) {
  while (Wire.available()) { // read all bytes received
    i2c_rcv = Wire.read();
  }
}

// requests data handler function
void dataRqst() {
  Wire.write(value_pot); // send potentiometer position
}

Thank you for the reply. Please find the code in the reply above. As i said, I want to blink an LED on the slave.

  1. I dont know if I should insert the LED Blink code in the slave or master.
  2. Let us say that the LED blink code needs to be on the slave, I dont know which line the LED blink code goes? Do I just paste it at the end?

Below is the LED blink code:

void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

Ty

The slave sketch performs a Wire.write, a send back of data.
Add a line making a digitalWrite to the led pin.
The slave code echoes data back to the master but that's an option that can be used later I suppose.

Ty. So I want to blink and LED connected to PIN 12.

So would the new slave code look like the following? (My PIN 12 code is on bold )

// Arduino slave sketch

#include <Wire.h>

byte i2c_rcv;               // data received from I2C bus
unsigned long time_start;   // start time in mSec
int stat_LED;               // status of LED: 1 = ON, 0 = OFF
byte value_pot;             // potentiometer position

//...................................................................................
// My LED Blink code that I inserted begins here.
//
//
void setup() {
  // initialize digital pin 12 as an output.
  pinMode(12, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}
//
//
// My LED Blink code that I inserted ends here.
//...............................................................................

void setup() {
  Wire.begin(0x08);           // join I2C bus as Slave with address 0x08

  // event handler initializations
  Wire.onReceive(dataRcv);    // register an event handler for received data
  Wire.onRequest(dataRqst);   // register an event handler for data requests

  // initialize global variables
  i2c_rcv = 255;
  time_start = millis();
  stat_LED = 0;

  pinMode(13, OUTPUT);        // set pin 13 mode to output
}

void loop() {

  value_pot = analogRead(A0); // read analog value at pin A0 (potentiometer voltage)

  // blink logic code
  if ((millis() - time_start) > (1000 * (float)(i2c_rcv / 255))) {
    stat_LED = !stat_LED;
    time_start = millis();
  }
  digitalWrite(13, stat_LED);
}

//received data handler function
void dataRcv(int numBytes) {
  while (Wire.available()) { // read all bytes received
    i2c_rcv = Wire.read();
  }
}

// requests data handler function
void dataRqst() {
  Wire.write(value_pot); // send potentiometer position
}

Then the Master sends a command "start blinking led" or "stop blinking led", and the Slave does the blinking ?
The sketch will be easier if you send a single byte over the I2C bus.

For example a single byte: 100 is start blinking and 101 is stop blinking.
The Slave needs to check a global variable and the global variable is set in the onReceive handler.

Master

#include <Wire.h>

void setup() 
{
  Wire.begin();
}

void loop() 
{
  // send command to start blinking
  Wire.beginTransmission( 0x08);
  Wire.write( 100);
  Wire.endTransmission();

  delay( 5000);

  // send command to stop blinking
  Wire.beginTransmission( 0x08);
  Wire.write( 101);
  Wire.endTransmission();

  delay( 5000);
}

Slave

#include <Wire.h>

volatile bool enableBlinking = false;

void setup() 
{
  Wire.begin(0x08);   // join I2C bus as Slave with address 0x08
  Wire.onReceive( receiveEvent);

  pinMode( 13, OUTPUT);
}

void loop() 
{
  if( enableBlinking)
  {
    digitalWrite( 13, HIGH);
    delay( 250);
    digitalWrite( 13, LOW);
    delay( 250);
  }
}

void receiveEvent( int howMany)
{
  if( howMany == 1)   // extra safety check, expecting just one byte
  {
    int x = Wire.read();
    switch( x)
    {
      case 100:
        enableBlinking = true;
        break;
      case 101:
        enableBlinking = false;
        break;
    }
  }
}

Not tested, there could be a few mistakes in it.

Eeh... Your last posted code contains 2 instances of loop(). That's wrong.
Regard reply #7, what do You want to do in the slave?

Ok Ty. I removed the loop and made it blink the hard way for now.
When the master sends a command I want the slave to Blink the LED on the slave's pin 12.
Ty

Please compile the code and make a test run. No offence but my feeling is that You're a bit new in coding. Evaluating untested desk produced code is a waist of time.
Now You show 2 instances of Setup. That's no, no.
You have a noname function starting as "void { digitalWrite(12...."
Compile and correct.

Sure. Will get back to you. Ty

Fine. Mind the speed limits....

Oh Wow! thanks a lor for the reply!!!
Let me digest that and will get back to you if I got questions.
(Will give St.Jude's Children's hospital $1 donation for your help)
Ty

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.