Connecting 4 Servos and an RTC Module to Arduino Nano


Below is a basic tutorial to get you started with how to connect four servo motors and an I2C RTC (Real-Time Clock) module to an Arduino Nano. This tutorial shows the necessary hardware, a basic wiring diagram, and code examples.

This pairs with the Rack Driven 7 Segment Display

Hardware Required

Optional Hardware

Servo Motors

Servo motors are used to control the angular position of an object. They have three pins:
  • VCC (usually red) - Connects to 5V
  • GND (usually black or brown) - Connects to GND
  • Signal (usually yellow or orange) - Receives the PWM control signal from the Arduino

DS3231 RTC Module

The DS3231 is a low-cost, highly accurate Real-Time Clock that communicates with the Arduino via the I2C protocol. It has four pins:
  • VCC - Connects to 5V
  • GND - Connects to GND
  • SDA - Connects to A4 (SDA) on the Arduino Nano
  • SCL - Connects to A5 (SCL) on the Arduino Nano

Wiring Diagram

Connecting Servo Motors

  1. Connect the VCC pin of each servo motor to the 5V power supply (note: The Arduino has a 5V output pin, however it can't supply enough current to drive your servo motors.  This is why you need an external power source.)
  2. Connect the GND pin of each servo motor to the GND pin on the Arduino Nano and the GND pin of your power supply.
  3. Connect the Signal pins of the servo motors to digital pins D2, D3, D4, and D5 on the Arduino Nano.

Connecting DS3231 RTC Module

  1. Connect the VCC pin of the RTC module to the 5V pin on the Arduino Nano.
  2. Connect the GND pin of the RTC module to the GND pin on the Arduino Nano.
  3. Connect the SDA pin of the RTC module to the A4 pin on the Arduino Nano.
  4. Connect the SCL pin of the RTC module to the A5 pin on the Arduino Nano.

Make sure you are powering your Arduino via the USB plug on the board.

Wiring Diagram Image

 

Alternative Option:

The Sunfounder Servo Board:

This is a board that the Arduino nano plugs into has a spot for up to 12 servos.  The RTC module can be connected with jumper cables.  Needs a 7-12V power supply (or DC adapter) to run, which powers both the Arduino, RTC, and the Servos.

https://www.sunfounder.com/products/servo-control-board

Note, these are the pin mappings from the nano to the sunfounder board:

Nano  SunFounder  |  Nano  Sunfounder
D2   -> 1                        A0  -> 7
D3   -> 2                        A1  -> 8
D4   -> 3                        A2  -> 9
D5   -> 4                        A3  -> 10
D6   -> 5                        A4  -> 11
D7   -> 6                        A5  -> 12

I2C Servo Board:

This board connects to Arduino via the i2c protocol (same as the RTC module).  It takes a 5V input, and can power up to 16 servo motors.  A great option, but requires a bit more advanced code. (https://geni.us/wNR5d)

 The Code:

 /*
  Engineezy 7 Segment Clock Code
  Uses a DS3231 Real Time Clock Module (I used this one: https://geni.us/rtKYLJT) and displays the time.
  Note you need the DS3231 Library from Andrew Wickert (https://github.com/NorthernWidget/DS3231)
  There is an example code in there to set the real time clock, then it just outputs the data!
*/
 
#include <Wire.h>
#include <DS3231.h>
#include <Servo.h>

RTClib myRTC;

Servo secondsOnes;
Servo secondsTens;
Servo minutesOnes;
Servo minutesTens;
Servo hoursOnes;
Servo hoursTens;

String inString = "";



void setup()
{
 // secondsOnes.write(0);
 // secondsTens.write(0);
  minutesOnes.write(0);
  minutesTens.write(0);
  hoursOnes.write(0);
  hoursTens.write(0);
 
  //secondsOnes.attach(2);
 // secondsTens.attach(3);
  minutesOnes.attach(2);
  minutesTens.attach(3);
  hoursOnes.attach(4);
  hoursTens.attach(5);

  Serial.begin(57600);
  Wire.begin();
  delay(500);
  Serial.println("Seven Segment Clock Begin");
}

void loop() {

  delay(1000);
  DateTime now = myRTC.now();

  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();


  //Split the secondss into Tens and Ones
  int curSecsTens = now.second()/10;
  int curSecsOnes = now.second()%10;
  //Convert it into angles
  int curSecsTensAngle = curSecsTens * 20;
  int curSecsOnesAngle = curSecsOnes * 20;
  //Write Seconds Motors
  secondsTens.write(curSecsTensAngle);
  secondsOnes.write(curSecsOnesAngle);
 
  //Split the Minutes into Tens and Ones
  int curMinsTens = now.minute()/10;
  int curMinsOnes = now.minute()%10;
  //Convert it into angles
  int curMinsTensAngle = curMinsTens * 20;
  int curMinsOnesAngle = curMinsOnes * 20;
  //Write Minutes Motors
  minutesTens.write(curMinsTensAngle);
  minutesOnes.write(curMinsOnesAngle);

 
  //Split the hours into Tens and Ones
  int curHoursTens = now.hour()/10;
  int curHoursOnes = now.hour()%10;
  //Convert it into angles
  int curHoursTensAngle = curHoursTens * 20;
  int curHoursOnesAngle = curHoursOnes * 20;
  //Write Hours Motors
  hoursTens.write(curHoursTensAngle);
  hoursOnes.write(curHoursOnesAngle);
 
}