Instructions for those learning to set up to create a clock in a 7-segment display for an arduino
Components Needed:
- Arduino (e.g., Uno, Nano)
- 7-segment 4-digit display (common anode or common cathode)
- DS3231 RTC (Real-Time Clock) module
- 4 Push buttons (for setting hours, minutes, etc.)
- Resistors (for buttons)
- Breadboard and jumper wires
Circuit Diagram:
Connect the DS3231 RTC Module to the Arduino:
- VCC to Arduino 5V
- GND to Arduino GND
- SDA to A4 (for Uno, Nano)
- SCL to A5 (for Uno, Nano)
Connect the 7-Segment Display:
- Remember the following details:
- Each segment (a-g) and the decimal point (dp) need to be connected to digital pins on the Arduino using current-limiting resistors (usually 220Ω or 330Ω).
- The common pins (COM) of the 4-digit display should be connected to digital pins on the Arduino to multiplex the display.
Connect the Buttons:
- Each button will be connected between a digital pin on the Arduino and GND.
- Use pull-up resistors (10kΩ) or the internal pull-up resistors in the Arduino code.
Example Circuit Connections (assuming common cathode display):
- Segments a-g: Digital pins 2-8
- Common pins for digits 1-4: Digital pins 9-12
- Buttons: Digital pins 13, A0, A1, A2
Arduino Code:
Here’s an example code to display the time from the DS3231 on the 7-segment display and to change the time using buttons.
cpp#include <Wire.h>
#include <RTClib.h>
// Define pins for 7-segment display
const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8};
const int digitPins[] = {9, 10, 11, 12};
// Define pins for buttons
const int buttonHour = 13;
const int buttonMinute = A0;
const int buttonSet = A1;
const int buttonMode = A2;
RTC_DS3231 rtc;
DateTime now;
int hour, minute;
bool setTimeMode = false;
void setup() {
Wire.begin();
rtc.begin();
// Set up segment pins as outputs
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Set up digit pins as outputs
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
}
// Set up button pins as inputs
pinMode(buttonHour, INPUT_PULLUP);
pinMode(buttonMinute, INPUT_PULLUP);
pinMode(buttonSet, INPUT_PULLUP);
pinMode(buttonMode, INPUT_PULLUP);
// Get the current time from RTC
now = rtc.now();
hour = now.hour();
minute = now.minute();
}
void loop() {
now = rtc.now();
if (setTimeMode) {
if (digitalRead(buttonHour) == LOW) {
hour = (hour + 1) % 24;
delay(200);
}
if (digitalRead(buttonMinute) == LOW) {
minute = (minute + 1) % 60;
delay(200);
}
if (digitalRead(buttonSet) == LOW) {
rtc.adjust(DateTime(now.year(), now.month(), now.day(), hour, minute, 0));
setTimeMode = false;
delay(200);
}
} else {
hour = now.hour();
minute = now.minute();
if (digitalRead(buttonMode) == LOW) {
setTimeMode = true;
delay(200);
}
}
displayTime(hour, minute);
}
void displayTime(int h, int m) {
int digits[] = {h / 10, h % 10, m / 10, m % 10};
for (int i = 0; i < 4; i++) {
digitalWrite(digitPins[i], LOW);
displayDigit(digits[i]);
delay(5);
digitalWrite(digitPins[i], HIGH);
}
}
void displayDigit(int num) {
const int numToSegments[] = {
// a b c d e f g
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], (numToSegments[num] >> i) & 0x01);
}
}
Explanation:
Initialization:
- Initializes the RTC module.
- Sets up pins for the 7-segment display and buttons.
Main Loop:
- Reads the current time from the RTC module.
- Checks if the set time mode is enabled.
- Increments hours or minutes when corresponding buttons are pressed.
- Updates the RTC with the new time when the set button is pressed.
- Displays the current time on the 7-segment display.
displayTime Function:
- Splits the hour and minute into individual digits.
- Displays each digit on the 7-segment display.
displayDigit Function:
- Converts the digit to the corresponding segments.
- Lights up the segments accordingly.
Notes:
- The delay between digit displays ensures proper multiplexing.
- Adjust pin assignments if needed based on your specific hardware setup.
This setup provides a functional real-time clock with the ability to set the time using buttons.