b o motor with i2c communication allowing to control the motors. TECHNICALLY we can add 128 motors ( 7 bit addressing). in a row
this week, our focus will be on learning how to establish connections between two or more boards, whether with or without wires. Understanding this concept is crucial as it enables us to facilitate communication between boards, which is essential for various projects including my final project.
individual assignment
this week i am making two boards powered by attiny 412 and i2c communicate it with a master attiny 1614 board made in WEEK 9 - OUTPUT DEVICES
the schematics
constraints
bom
teh board making process is explained in the WEEK 4 - ELECTRONICS PRODUCTION.
routing
the boards are woking independently. but i was not able to establish i2c with . reason i found was that i pulled down line with RESISTORS instead of pull up. i removed the resistors . EVEN THOUGH i need to pullup the line. i shoudl figure it out
so what i have done is i added pull up in WEEK 9 - OUTPUT DEVICES master board. which sorted the issue .
and i REFERRED to what done beforehand . got sample code for from saheeen's documentation.
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
}
byte x = 0;
void loop() {
Wire.beginTransmission(8); // transmit to device #8
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
x++;
delay(500);
}
the student code i took for reference
//
// Slave-reader
//
// Wire Slave Receiver
//
// by Nicholas Zambetti < http://www.zambetti.com> and modified by Saheen Palayi 06/06/2022
//
// This work may be reproduced, modified, distributed,
// performed, and displayed for any purpose, but must
// acknowledge this project. Copyright is retained and
// must be preserved. The work is provided as is; no
// warranty is provided, and users accept all liability.
//
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// Created 29 March 2006 by Nicholas Zambetti
// This example code is in the public domain.
//Added software serial for Attiny84
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
master code
for the i2c wiring i made this connector with 2*2 conn header
made the connection wire
the setup
the circuit diagram
SUCCESSFUL debugging
yes i was finally able to debug successfully.
this is done using the help of chat gpt. the prompt log is like
Prompt: "I have motors on pin 4 of both slaves. I have established I2C communication between master and slave devices. Now, I want to control the motors using serial input to the master. Inputting the slave address (8 or 9) and a command (1 or 0) should turn the motor on or off."
ChatGPT Response: ChatGPT provided an outline of the steps and sample code for setting up the master and slave Arduinos to achieve the desired motor control.
Prompt: "I need an example of how the master sends the command to the slaves and how the slaves interpret these commands to control the motors."
ChatGPT Response: ChatGPT gave detailed Arduino code for both the master and the slave devices, including how to parse serial inputs on the master, send commands to the appropriate slave, and control the motor on the slave.
Prompt: "I've set up the master and slave code. How can I test if the I2C communication is working properly before integrating motor control?"
ChatGPT Response: ChatGPT suggested using basic I2C communication test code to ensure the master can send and the slaves can receive messages.
Prompt: "How do I integrate the motor control part after confirming the communication works?"
ChatGPT Response: ChatGPT provided the complete integrated code for the master and the slave to parse commands and control the motors based on the serial input.
we are using the Serial Monitor to send commands to the master Arduino, which then relayed the commands to the appropriate student Arduino. The commands included the student address and the desired state of the motor (on or off).
Student 1 Code (Device Address 8)
#include <Wire.h>
const int motorPin = 4;
void setup() {
pinMode(motorPin, OUTPUT);
Wire.begin(8); // join I2C bus with address #8
Wire.onReceive(receiveEvent); // register event
}
void loop() {
// Nothing to do here
}
// function that executes whenever data is received from master
void receiveEvent(int howMany) {
if (Wire.available()) {
byte command = Wire.read();
if (command == 1) {
digitalWrite(motorPin, HIGH); // turn motor on
} else if (command == 0) {
digitalWrite(motorPin, LOW); // turn motor off
}
}
}
Student 2 Code (Device Address 9)
#include <Wire.h>
const int motorPin = 4;
void setup() {
pinMode(motorPin, OUTPUT);
Wire.begin(9); // join I2C bus with address #9
Wire.onReceive(receiveEvent); // register event
}
void loop() {
// Nothing to do here
}
// function that executes whenever data is received from master
void receiveEvent(int howMany) {
if (Wire.available()) {
byte command = Wire.read();
if (command == 1) {
digitalWrite(motorPin, HIGH); // turn motor on
} else if (command == 0) {
digitalWrite(motorPin, LOW); // turn motor off
}
}
}
Master Code
#include <Wire.h>
void setup() {
Wire.begin(); // join I2C bus as master
Serial.begin(9600); // start serial for output
Serial.println("Enter commands in the format: <address> <command>");
Serial.println("For example, '8 1' to turn on motor on student 8");
}
void loop() {
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
int spaceIndex = input.indexOf(' ');
if (spaceIndex > 0) {
int address = input.substring(0, spaceIndex).toInt();
int command = input.substring(spaceIndex + 1).toInt();
sendCommand(address, command);
}
}
}
void sendCommand(int address, int command) {
Wire.beginTransmission(address);
Wire.write(command);
Wire.endTransmission();
Serial.print("Sent command ");
Serial.print(command);
Serial.print(" to student ");
Serial.println(address);
}
receiveEvent
function was registered to handle incoming data from the master.receiveEvent
function read the command. If the command was 1
, the motor turned on. If the command was 0
, the motor turned off.<address> <command>
. For example, 8 1
meant sending command 1
to the student at address 8
.sendCommand
function sent the specified command to the designated student address via I2C.This setup allowed us to control multiple student devices from a single master device using the I2C protocol.
This involves a lot of debugging. I've gained confidence that I can make things right even in the worst situations, especially when it comes to working with boards.