-1

I'm just an amateur and this might be a stupid question that requires a simple fix, but I can't find a solution online. I'm working on a project in which I need to use a servo driver and a LCD screen. The problem is that, according to the libraries' docs, both devices need to be plugged into the A4 and A5 analog pins. Also, I can't find where the used pins are declared in the libraries' files (for what I know there may not be the need to do so, I've never modified a library). Could you help me?

For context, I'm using the HCPCA9685.zip library for the driver and the LiquidCrystal_I2C.zip one for the LCD display. The code I'm working on is:

//VERSION WITH BUTTONS AND DISPLAY, NO STEPPER

//Libraries
  #include <HCPCA9685.h> //for the driver
  #include <Wire.h>  //For the display
  #include <LiquidCrystal_I2C.h> // for the display
  #include <Keypad.h> //for the keypad

//Mantainance
  #define  I2CAdd 0x40
  const byte ROWS = 4; //defines rows and columns of the keypad
  const byte COLS = 4;
  HCPCA9685 HCPCA9685(I2CAdd); //Activates the driver
  char keys[ROWS][COLS] = { //Array to represent the buttons on the keypad
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
  };
  byte rowPins[ROWS] = {9, 8, 7, 6}; //pins the keypad is connected to
  byte colPins[COLS] = {5, 4, 3, 2};
  Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // creates the keypad object
  LiquidCrystal_I2C lcd(0x27, 16, 2); //creates the LCD object


//Costants
  int servo1_min = 0; //Servos' min and mac positions
  int servo1_max = 450;
  
  int servo2_min = 450;
  int servo2_max = 0;  //SERVO2 POS = 450 - SERVO1 POS

  int servo3_min = 400;
  int servo3_max = 0;

  int servo4_min = 0;
  int servo4_max = 370;

  int servo5_min = 0;
  int servo5_max = 400;
  
  int servo6_min = 0;
  int servo6_max = 300;

void setup() 
  {
  HCPCA9685.Init(SERVO_MODE); //Wakes up the driver
  HCPCA9685.Sleep(false);
  Serial.begin(9600); //Serial monitor adress
  lcd.backlight(); // Setup LCD with backlight and initialize
  lcd.init(); 

  Serial.print("1: Wake up\n2:...");

  lcd.clear();      
  lcd.setCursor(0, 0); 
  lcd.print("LCD start");
  }
void loop() 
  {
  char key = keypad.getKey(); // Read the key from the keypad
  if (key == '1') {
  lcd.clear();      
  lcd.setCursor(0, 0); 
  lcd.print("Wake up");
  delay(3000);
  wakeup();
  }
  }
//User defined functions
  void wakeup()
  {
  lcd.clear();      
  lcd.setCursor(0, 0); 
  lcd.print("Wakeup strarting");
  delay(3000);
  HCPCA9685.Servo(0, servo1_min);
  HCPCA9685.Servo(2, servo2_min);
  delay(2000);
  HCPCA9685.Servo(4, servo3_min);
  delay(2000);
  HCPCA9685.Servo(6, servo4_min);
  delay(2000);
  HCPCA9685.Servo(8, servo5_min);
  delay(2000);
  HCPCA9685.Servo(10, servo6_min);
  }

I've tried expiciting the input with analogRead(), bit it really didn't seem to work

2 Answers2

0

In this case A4 and A5 are serving their alternate functions as SDA and SCL pins for the I2C bus. I would suggest you read up on I2C and how it works. Many things can share those pins for communication as long as they have different I2C addresses.

Delta_G
  • 2,927
  • 2
  • 9
  • 15
0

You didn't mentioned which Arduino board you are using, but assuming it is an Arduino Uno. A4 and A5 in Arduino Uno are just a label specifying that those pin are analog-capable and they are assigned to ADC channel 4 and channel 5 when using as analog pin. A4 and A5 are also defined as SDA and SCL for I2C communication, they can also be used as digital pin 18 and 19 as well. They are not used as analog pins in this case and therefore no analogRead() to be called.

I2C is a bus, meaning that many devices can be connected to the same bus as long as they have different I2C addresses, HCPCA9685 is on address 0x40 and LCD is on 0x27 in your code.

hcheung
  • 3,377
  • 3
  • 11
  • 23
  • Thank you very much!! Now I understand. I'm sorry I forgot to specify the board, it was actually an Arduino UNO. Thank you so much again, your answer was really usefoul – Martina A Jul 23 '23 at 15:28