Issues using I2C with only one device connected

I'm doing a simple test to understand I2C for a future project. I have a Mega2560 R3 master connected to an Uno R3 slave, where the slave is measuring a potentiometer input and sending the data to my master to control a servo. I can only plug one controller into my computer at a time, so the master has to power the slave. Even with the code uploaded to both controllers, though, it doesn't work. How do I solve this? A photo of the setup and my code are included.

Master Code:

#include <Wire.h>
#include <Servo.h>

Servo myServo;
int angle;

void setup() {
  Wire.begin();
  myServo.attach(2);

  Serial.begin(9600);
}

void loop() {
  angle = Wire.read();

  Serial.print("angle: ");
  Serial.println(angle);

  myServo.write(angle);
  delay(15);
}

Slave Code:

#include <Wire.h>

int const potPin = A0;
int potVal;
int angle;

void setup() {
  Wire.begin(0x08);
  Serial.begin(4800);
}

void loop() {
  potVal = analogRead(potPin);
  Serial.print("potVal: ");
  Serial.print(potVal);

  angle = map(potVal, 0, 700, 0, 179);
  Serial.print(", angle: ");
  Serial.println(angle);

  Wire.write(angle);
  delay(15);
}

My Setup:

  • Don’t power motors from your Arduino.

Yes, the gray wire is connected to both board grounds.

This is literally a circuit from the official Arduino projects book that came with the old starter kit.

  • Yes, and their depiction is wrong.
    The Arduino should not be use as a power supply to inductive loads.
    You can use it for low current displays, sensors etc., but not motors.

Can I operate a servo with Arduino? This is a 5V servo that came with the starter kit. I've done multiple projects with it and haven't had any issues.

I meant that operating this servo with my arduino has not been an issue in the past. I also tested it just now and it works just fine, so its definitely an issue with the I2C rather than the servo itself.

  • Eventually you will damage electronics.

  • Buy a 4 X AA battery holder, common the 0v line to the Arduino GND.
    +6v goes to the positive of the servo (maybe through a switch if you want).

  • Disconnect the battery from the servo when you are finished using it.

Maybe these examples will help.

I combine the slave_receiver and slave_sender scripts.
Also master_reader and master_writer.

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