Project tutorial

4-Stroke Digital Clock With Arduino © CC BY-NC-ND

This digital clock driven by Arduino is a fun emulator of 4-stroke engine where the digits of hours & minutes represent the pistons moving.

  • 13,696 views
  • 16 comments
  • 44 respects

Components and supplies

Apps and online services

About this project

Introduction

The "4-Stroke Digital Clock" driven by Arduino is a fun emulator of an internal combustion engine in a digital clock. Hours and minutes digits represent the pistons moving and with a precise RPM control (100 to 800). The RPM are shown by two columns in the center of display.

Another interesting visual information is the firing sequence "1-3-4-2" of cylinders. Spark starts when the piston is at the top of cylinder in the compression cycle. The code is a good exercise for managing dot matrix pixels and for introducing a simple animation feature.

Video

The 4-Stroke Internal Combustion Engine

The engine model used in this project is an I4 (four cylinders in line) in 4-Stroke cycle. The 4-stroke internal combustion engine works in 4 steps (Intake-Compression-Combustion-Exhaust) as illustrated in the diagram below:

Note: More basic information of Internal Combustion Engines can be find at Wikipedia.

The Clock

In this project the digits of Hours and Minutes simulate the pistons moving and the spark is highlighted in correct time for all cylinders.

The rotation is accurately calculated to represent the actual speed and the value is adjusted by a rotary potentiometer from 100 to 800 RPM (Revolutions Per Minute).

Material List

  • Arduino UNO R3
  • LED Dot Matrix with MAX7219
  • DS1307 RTC (Real Time Clock)
  • Rotary Potentiometer - 10K Ohms
  • Breadboard
  • Print Template
  • Jumpers

Assembly

The assembly is very simple but you must follow the attached diagram with attention.

Code

_04_Stroke_Digital_Clock_With_MAX72XX_LED_Matrix_and_RTC_V1_1.inoArduino
Code for Arduino UNO
/*
   Project:   4-Stroke Digital Clock
   Author:    LAGSILVA
   Hardware:  Arduino UNO-R3 / LED Matrix with MAX72XX / RTC
   Date:      02.Feb.2019
   Revision:  1.1
   License:   CC BY-NC-ND 4.0
              (Attribution-NonCommercial-NoDerivatives 4.0 International)
*/

#include <LedControl.h>                   // Library for LED Matrix - MAX72XX
#include <Wire.h>                         // Library for DS1307RTC - Pins of Arduino UNO: A4 (SDA), A5 (SCL)
#include <DS1307RTC.h>                    // Library for Real Time Clock
#include <Time.h>                         // Library for Time
#include <TimeLib.h>

// Global Variables
byte hh, mm;                              // Hour, Minute
byte uH, uM, dH, dM;                      // Units Hour, Units Minute, Teens Hour, Teens Minute
byte p = 0;                               // Plot Mode (Position of Digits: Up/Down)
int RPM = 0, lastRPM = 0, pinRPM = 0;     // RPM, Last value of RPM, Analog Pin for RPM
byte seq = 1;                             // Start Fire sequence

/*
  Pin numbers do Arduino para conexao com o MAX72XX (matriz de LED com controlador MAX72XX)
  pin 5 is connected to the DataIn (DIN)
  pin 6 is connected to LOAD (CS)
  pin 7 is connected to the CLK (CLK)
*/

LedControl lc = LedControl(5, 7, 6, 2);  // LedControl(int DIN, int CLK, int CS, int numDevices)


// Array of Digits - (0 to 9) - Size 5x3

byte num[10][3] = {

  {0xF8, 0x88, 0xF8}, // 0
  {0x00, 0xF8, 0x40}, // 1
  {0xE8, 0xA8, 0xB8}, // 2
  {0xF8, 0xA8, 0x88}, // 3
  {0x78, 0x20, 0xE0}, // 4
  {0xB8, 0xA8, 0xE8}, // 5
  {0xB8, 0xA8, 0xF8}, // 6
  {0xC0, 0xB8, 0x80}, // 7
  {0xF8, 0xA8, 0xF8}, // 8
  {0xF8, 0xA0, 0xE0}, // 9

};


void setup() {

  setSyncProvider(RTC.get);              // Function to read RTC (Real Time Clock)
  setSyncInterval(60);                   // Set the number of seconds between re-sync
  //setTime(8, 19, 30, 10, 02, 2018);     // Set the Time and Date (hour, minute, second, day, month, year)
  //RTC.set(now());                       // Set the RTC time

  // Setup of Display "0"
  lc.shutdown(0, false);                // Wakeup Display "0"
  lc.setIntensity(0, 4);                // Set the Brightness of Display (0 to 15)
  lc.clearDisplay(0);                   // Clear Display "0"

  // Setup of Display "1"
  lc.shutdown(1, false);                // Wakeup Display "1"
  lc.setIntensity(1, 4);                // Set the Brightness of Display (0 to 15)
  lc.clearDisplay(1);                   // Clear Display "1"

}


void loop() {

  hh = hour();
  mm = minute();

  uH = hh % 10;
  dH = hh / 10;

  uM = mm % 10;
  dM = mm / 10;


  // Plot Hours

  for (byte k = 0; k < 3; k++) {

    lc.setRow(0, k + 5, num[dH][k] >> (p + 2));
    lc.setRow(0, k + 1, num[uH][k] >> (3 - p));

  }


  // Plot Minutes

  for (byte k = 0; k < 3; k++) {

    lc.setRow(1, k + 4, num[dM][k] >> (3 - p));
    lc.setRow(1, k , num[uM][k] >> (p + 2));

  }


  // Plot Fire Sequence: 1-3-4-2 (4 cyl: 1-3-4-2 or 1-2-4-3 or 1-4-3-2)

  if (seq == 1) {                               // Fire Cyl #1
    lc.setLed(0, 6, 0, true);
  }

  if (seq == 2) {                               // Fire Cyl #3
    lc.setLed(1, 5, 0, true);
  }

  if (seq == 3) {                               // Fire Cyl #4
    lc.setLed(1, 1, 0, true);
  }

  if (seq == 4) {                               // Fire Cyl #2
    lc.setLed(0, 2, 0, true);
  }

  seq = seq % 4 + 1;


  // RPM reading

  RPM = analogRead(pinRPM);                     // Potentiometer reading

  RPM = map(RPM, 0, 1023, 1, 9);

  if (RPM <= 8) {
    lc.setLed(1, 7, 8 - RPM, true);
    lc.setLed(0, 0, 8 - RPM, true);
    delay(300 / RPM);                           // Speed setup by potentiometer (100 - 800 RPM)

    if (lastRPM != RPM) {
      lc.setRow(0, 0, 0);
      lc.setRow(1, 7, 0);
      lastRPM = RPM;
    }

  }

  p = !p;                                       // Switch the position of digits (Up x Down)


}

Custom parts and enclosures

Template for Dot Matrix LED display
Template must be printed in original size.

Schematics

Breadboard sketch
Breadboard sketch

Comments

Similar projects you might like

Digital & Binary Clock In 8 Digits x 7 Segments LED Display

Project showcase by LAGSILVA

  • 1,827 views
  • 2 comments
  • 9 respects

Digital And Binary Clock With Two LED Matrix And RTC

Project tutorial by LAGSILVA

  • 6,117 views
  • 10 comments
  • 27 respects

Complete Digital Clock Including Alarm and Motion Sensor

Project tutorial by LAGSILVA

  • 12,678 views
  • 11 comments
  • 50 respects

Digital Clock with Arduino, RTC and Shift Register 74HC595

Project tutorial by LAGSILVA

  • 20,730 views
  • 16 comments
  • 46 respects

Analog Clock with LED Matrix and Arduino

Project tutorial by LAGSILVA

  • 12,237 views
  • 8 comments
  • 39 respects

Digital Clock with Mirrored Display Driven by Accelerometers

Project showcase by LAGSILVA

  • 14,365 views
  • 1 comment
  • 38 respects
Add projectSign up / Login