Getting Started with Arduino! – Chapter Eight

This is part of a series titled “Getting Started with Arduino!” by John Boxall – A tutorial on the Arduino microcontrollers. The first chapter is here, the complete index is here.

In this chapter we will continue to examine the features of the DS1307 real time clock, receive user input in a new way, use that input to control some physical movement, then build a strange analogue clock. So let’s go!

Recall from chapter seven, that the DS1307 is also has an inbuilt square wave generator, which can operate at a frequency of 1Hz. This is an ideal driver for a “seconds” indicator LED. To activate this you only need to send the hexidecimal value 0x10 after setting the date and time parameters when setting the time. Note this in line 70 of the solution for exercise 7.1. This also means you can create 1Hz pulses for timing purposes, an over-engineered blinking LED, or even an old-school countdown timer in conjunction with some CMOS 4017 ICs.

For now, let’s add a “seconds” LED to our clock from Exercise 7.1. The hardware is very simple, just connect a 560 ohm resistor to pin 7 of our DS1307, thence to a normal LED of your choice, thence to ground. Here is the result:

Not that exciting, but it is nice to have a bit more “blinkiness”.

Finally, there is also a need to work with 12-hour time. From the DS1307 data sheet we can see that it can be programmed to operate in this way, however it is easier to just work in 24-hour time, then use mathematics to convert the display to 12-hour time if necessary. The only hardware modification required is the addition of an LED (for example) to indicate whether it is AM or PM. In my example the LED indicates that it is AM.

Exercise 8.1

So now that is your task, convert the results of exercise 7.1 to display 12-hour time, using an LED to indicate AM or PM (or two LEDs, etc…)

Here is my result in video form:

and the sketch.

OK then, that’s enough about time for a while. Let’s learn about another way of accepting user input…

Your computer!

Previously we have used functions like Serial.print() to display data on the serial monitor box in the Arduino IDE. However, we can also use the serial monitor box to give our sketch data. At first this may seem rather pointless, as you would not use an Arduino just to do some maths for you, etc. However – if you are controlling some physical hardware, you now have a very simple way to feed it values, control movements, and so on. So let’s see how this works.

The first thing to know is that the serial input has one of two sources, either the USB port (so we can use the serial monitor in the Arduino IDE) or the serial in/out pins on our Arduino board. These are digital pins 0 and 1. You cannot use these pins for non-serial I/O functions in the same sketch. If you are using an Arduino Mega the pins are different, please see here.  For this chapter, we will use the USB port for our demonstrations.

Next, data is accepted in bytes (remember – 8 bits make a byte!). This is good, as a character (e.g. the letter A) is one byte. Our serial  input has a receiving buffer of 128 bytes. This means a project can receive up to 128 bytes whilst executing a portion of a sketch that does not wait for input. Then when the sketch is ready, it can allow the data to serially flow in from the buffer. You can also flush out the buffer, ready for more input. Just like a … well let’s keep it clean.

Ok, let’s have a look. Here is a sketch that accepts user input from your computer keyboard via the serial monitor box. So once you upload the sketch, open the serial monitor box and type something, then press return or enter. Enter and upload this sketch:

// Example 8.1

int character = 0; // for incoming serial data
void setup() 
void loop() 
  if (Serial.available() > 0) 
    character =;


Here is a quick video clip of it in operation:

So now we can have something we already know displayed in front of us. Not so useful. However, what would be useful is converting the keyboard input into values that our Arduino can work with.

Consider this example. It accepts a single integer from the input of serial monitor box, converts it to a number you can use mathematically, and performs an operation on that number. Here is a shot of it in action:


If you are unsure about how it works, follow the sketch using a pen and paper, that is write down a sample number for input, then run through the sketch manually, doing the computations yourself. I often find doing so is a good way of deciphering a complex sketch. Once you have completed that, it is time for…

Exercise 8.2

Create a sketch that accept an angle between 0 and 180, and a time in seconds between 0 and (say) 60. Then it will rotate a servo to that angle and hold it there for the duration, then return it to 0 degrees. For a refresher on servo operation, visit chapter three before you start.

Here is a video clip of my interpretation at work:

So now you have the ability to generate user input with a normal keyboard and a PC. In the future we will examine doing so without the need for a personal computer…

Finally, let’s have some fun by combining two projects from the past into one new exercise.

Exercise 8.3

Create an analogue clock using two servos, in a similar method to our analogue thermometer from chapter three. The user will set the time (hours and minutes) using the serial monitor box.

Here is a photo of my example. I spared no expense on this one…


Here is a video demonstration. First we see the clock being set to 12:59, then the hands moving into position, finally the transition from 12:59 to 1:00.

If you had more servos and some earplugs, a giant day/date/clock display could be made… Nevertheless, we have had another hopefully interesting and educational lecture. Or at least had a laugh. Now onto chapter nine.


Have fun and keep checking into Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

17 thoughts on “Getting Started with Arduino! – Chapter Eight

  1. Stu

    In the video Exercise 8.3, the hour servo seems to bounce a little bit when it moves back to 1 – is this due to the ‘cheapness’ of the servos used or some kind of interference?

  2. Richard james Clapham

    Hi could you shed some light on how to delimit or split data coming into the serial port to control servo’s? trying to send bytes separated by a tab or “,” in processing and read in arduino, but separating the data is proving to be a difficult task. Any advice is great Thanks.

      1. Richard james Clapham

        Hi John, thanks for the link, looks great. I’m onto your I2C tutorial now. Regards Richard

  3. Pingback: Build your own Arduino-based Laser Clock « freetronicsblog

  4. Jordan Whitley

    Do you happen to have the sketch available for inputting serial data and getting the servo to move to the corresponding position? Exercise 8.2 in particular, I do not see it anywhere on your web site. Thank you very much!

  5. Jordan Whitley

    #define SERVO_1_PIN 2

    Servo g_servo1;

    void setup()
    pinMode(SERVO_1_PIN, OUTPUT);
    Serial.println(“Enter degrees and press ‘s’ to send it to servo.”);

    void loop()
    static int val = 0;

    if (Serial.available())
    char ch =;

    case ‘0’…’9′:
    val = val * 10 + ch – ‘0’;
    case ‘s’:
    Serial.print(“Servo1 is set to: “);
    Serial.print(val, DEC);
    Serial.println(” degrees”);
    val = 0;


    Does something like this look okay to you?

      1. Jordan Whitley

        I got it to go to a specific angle, so that part works, I just can’t figure out the timing section of it.

  6. Steve56

    Hi John,

    Thank you for your excellent tuts, all is going well so far.

    Instead of using the serial monitor to send data to the Arduino, I’m looking at a better interface via Excel and VBA as that is fairly portable.

    Do you (or any readers) know of a good tutorial on interfacing Arduinos with VBA ?

    I have been googling of course, but no doubt someone has done it before which would save some time.


      1. Steve56

        It turned out easier than expected.
        I put a MSCOMM control on a Excel form and used the following code

        Private Sub Open_Arduino_Port()
        MSComm1.CommPort = Trim(TextBox3.Text)

        MSComm1.Settings = “9600,N,8,1”
        MSComm1.InputLen = 0
        MSComm1.PortOpen = True

        End Sub

        Private Sub Write_Arduino_Data()

        data = Trim(TextBox1.Text)

        MSComm1.Output = data

        End Sub

        Private Sub Close_Arduino_Port()

        MSComm1.PortOpen = False
        Label4.Caption = “Closed”

        End Sub
        TextBox1 contains a number (0-255) to control the brightness of an LED.

        Opening or closing the serial port resets the Arduino, so only do it once

        Type a number from 0-255 into a TextBox (TextBox1 in my case), then call Write_Arduino_Data with a command button.

        The Arduino sketch to process the data follows
        int outPort=3 ;

        int nBright = 0 ;
        int i=0;

        void setup()
        Serial.begin(9600) ; // set up the serial monitor

        void loop()

        // Serial.flush(); // clear any ‘junk’ out of the serial buffer before waiting
        while (Serial.available() == 0)
        // do nothing until something enters the serial buffer
        while (Serial.available() > 0)
        nBright = Serial.parseInt();

        Serial.print(“You entered: “);

        // Control the brightness of a LED attached to Outport
        // First make sure the number is 0-255

        if(nBright 255){nBright=255;}

        analogWrite(outPort,nBright) ;


        Hope this helps anyone trying to do the same thing.

Comments are closed.