Servo Clock Calibration

The first servo clock was built using a name-brand servo known for its linearity.  The second iteration was built using a low-cost mini-servo.  Its linearity was not as good; uncorrected, the time readout was not acceptable.

In the ideal case, locating the delay value at the starting and ending points would be enough for the clock to read properly across the range.  The angle of rotation would vary linearly across the scale.  If the servo is not extremely linear, the pointer will not always point to correct location in the scale.  As an example, if the actual time was 6:05, a non-linear servo might point at 5:55.

The low cost servo has some areas that are highly non-linear.  At one point in the scale, the difference between quarter-hour marks was a 20 ms change in pulse width.  At a different point in the scale, the change was over 40 ms.


Improved Method

It's most important that the servo clock pointer line up correctly with the scale marks at quarter-hour intervals.  The exact position between the marks isn't as important.  This improved method calibrates each quarter-hour mark, and linearly interpolates between the marks.  A look-up table approach is used to set the position at each of the 720 minutes across the span.

The hardware used for this clock is the TAP-28 board with an 18F242 microcontroller.  The hardware is not critical and other implementations can be used.  This code was developed using Swordfish.

A potentiometer is used to set the servo position to read the calibration values.  I used a 10-turn pot but a regular single turn pot or a multi-turn trim pot could be used.  The value of the pot isn't critical - anything between 1k and 100k ohms will work fine.  Make sure the pot is linear taper. 



1.  Mount the servo in position behind the clock scale but do not install the pointer yet.

2.  Connect the pot to PORTA.0.  The wiper terminal goes to the port pin.  The ends of the pot are connected to +5V and ground.

3.  Connect the servo to PORTC.2.

4.  Load the calibration code to the PIC.


* Name : Servo Test and Calibration Program *
* Author : Jon Chandler *
* Notice : Copyright (c) 2010 Jon Chandler *
* : All Rights Reserved *
* Date : 1/29/2010 *
* Version : 1.0 *
* Notes : Program to test and calibrate servos. Servo *
* position is controlled by a potentiometer *
* connected to ADC(1). Position is sent via UART *
* to the PC to read the delay at each position. *
* This program uses the TAP-28 Board with a PIC *
* 18F242. *

'Servo output = PORTC.2
  'UART output = UART connector
  'Pot connection = PORTA.1
  'UART Baudrate = 9600

Device = 18F242
Clock = 20
Include "utils.bas"
Include "USART.bas"
Include "convert.bas"
Include "ADC.bas"
Dim Posit As Word
Dim ADVal As Word
Function ADInAsVolt() As Word
result = (ADC.Read(1) + 1) * 500 / 1024
End Function
ADCON1 = %10000000
ADCON0.7 = 1
ADCON0.6 = 1
ADC.RightJustify = True
While 1=1
ADVal = ADInAsVolt
Posit = 500 + 4* ADVal
USART.Write("ADC Value = ", DecToStr(ADVal)," Posit: ",DecToStr(Posit), 13,10)
High(PORTC.2) 'create servo pulse, repeatedly send approximately
  DelayUS(Posit) 'every 200 milliseconds
  Low (PORTC.2)


5.  Connect a PICkit2 to the UART connector and launch the PICkit UART tool on a PC.  Alternatively, use a UART (5V TTL) - RS232 converter or a UART - USB converter and terminal software on the PC.  Set the baud rate to 9600/N/1.

6.  Power the board using the PICkit or connect a 5V power supply.

7.  Adjust the pot and verify that the servo turns.  Adjust the pot until the position readout on the PC is 1500.  This is the middle of the servo's range.


8.  Install the pointer on the servo's splined shaft, as close as possible to midscale on the clock face.

9.  Adjust the pot to position the pointer over the left hand 12.  Read the position on the PC and record the position in the spreadsheet.


10  Adjust the pot to position the pointer over the next mark on the clock face and record the position.  Continue the process for each mark on the clock face.



This completes the calibration measurements.


Using The Results

Now, open the clock program in Swordfish.  Select the spreadsheet region from D2 - R13.  Press <CTRL> C to copy this region.  Paste this tabular data into the constant data in Swordfish, replacing the existing data.

* Name : Servo_Clock 2.bas *
* Author : Jon Chandler *
* Notice : Copyright (c) 2010 Jon Chandler *
* : All Rights Reserved *
* Date : 3/11/2010 *
* Version : 1.0 *
* Notes : Servo-driven geeky clock with Real Time Clock *
* : fucntions based on Warren Schroeder's methods *
* : using PR2 Free Running Timer *
* *
* *
* Presented to: by Jon Chandler *
* *
* Timekeeping depends on a 20 MHz crystal *
* *
* This version uses a small servo from Hobby Partz and the TAP-28 *
* board with a PIC18F242 installed. *
* A constant array is used as a lookup table to overcome non-linearity*
* in the servo. Calibrated position values used for each minute. *
* See for more information. *

Device = 18F242
Clock = 20
Include "utils.bas"
Include "USART.bas"
Include "convert.bas"
Const TimeValues(721) As Word = (
//delete the values below and paste in the calculated resuls from the spreadsheet.
//Paste values from spreadsheet above.
Dim Set_Fast As PORTB.5 'S1 = Fast Set
  Dim Set_Slow As PORTB.4 'S2 = Slow Set

Dim Servo As PORTC.2 '(bottom 3 pin connector)

For One Second Update:
8MHz Fosc = 2MHz internal clock = 0.5us per cycle (timer count)
Use 16-bit Timer1, No Prescaler
Set CCPR1 = 50000; Timer1 resets on match every 50000 counts = 25000us
Each Timer1 reset requires 1 cycle compensation... so set CCPR1 = 49999
40 interrupts x 25000us each = 1 second

For One Second Update: 20 MHz
20MHz Fosc = 5MHz internal clock = 0.2us per cycle (timer count)
Use 16-bit Timer1, No Prescaler
Set CCPR1 = 50000; Timer1 resets on match every 50000 counts = 10000us
Each Timer1 reset requires 1 cycle compensation... so set CCPR1 = 49999
100 interrupts x 10000us each = 1 second

Dim C1 As Word Absolute $0FBE ' CCPR1L + CCPR1H
  Dim Int_Counter As LongWord

Dim update As Boolean
Dim secs,mins,hrs As Word
Dim Posit As Word
Dim PositLeft As Word
Dim PositRight As Word
Interrupt RTC()
If Int_Counter = 0 Then
Int_Counter = 100 ' each interrupt = 10000us x 100 int's = 1 second
  update = true

End If
PIR1.2 = 0 ' clear CCP1 interrupt flag
  End Interrupt

Sub Clock24()
Dim clk As String
If secs = 43200 Then ' check each tally for rollover
  secs = 0

End If
update = false
mins = secs/60
Posit = TimeValues(mins) 'select constant value for each minute
  USART.Write("Seconda = ", DecToStr(secs)," Minutes = ", DecToStr(mins)," Posit: ",DecToStr(Posit), 13,10)

'UART output can be used to monitor clock operation

'Note: For the servo used, maxium rotation was counter-clockwise.
  End Sub

Sub Initialize() 'timekeeping routine
  ADCON1 = 15

secs = 0
mins = 0
hrs = 0
Int_Counter = 100
update = false
INTCON = 192 ' enable GIE & PEIE
  T1CON = 0 ' no prescaler timer OFF
  TMR1H = 0 ' clear TMR1
  TMR1L = 0

CCP1CON = 11 ' enable special trigger event
  C1 = 49999 ' set match value
  PIE1.2 = 1 ' enable CCP1 interrupt
  PIR1.2 = 0 ' clear CCP1 interrupt flag
  T1CON.0 = 1 ' Timer1 ON
  Enable(RTC) ' enable jump to RTC ISR
  End Sub

Sub settime()
End Sub
PositLeft = 2292
PositRight = 984
Posit = 1500
secs = 0 'initialize to 12:00.

While 1=1
If update = true Then
Clock24 ' update 24H Clock output
  End If

High(Servo) 'create servo pulse corresponding to position and
  DelayUS(Posit) 'repeatedly send approximately every 200 milliseconds
  Low (Servo)

'clock set procedure
  If Set_Fast = 0 Then 'increment seconds at 100x and free run
  secs = secs +900

update = true
If Set_Slow = 0 Then 'increment seconds at 10x and free run
  secs = secs +9

update = true


Compile the program and load it into the PIC.



This picture shows the servo clock being calibrated.  The 10-turn pot is in the gray box at the bottom, and is connected to PORTA.1 on the TAP-28 board.  The servo cable is the yellow/orange/brown cable disappearing at the top right, connected to the PORTC.2 connector on the TAP-28 board.  The only thing better than one PICkit-2 is two PICkit-2s.  The lower one with the white cable is connected to the ICSP connector on the TAP-28 and the upper one is connected to the UART connector to monitor position readings.  The TAP-28 board is powered by one of the PICkit-2s.  A single PICkit-2 could be switched between connectors also.



This is a closeup of the TAP-28 board.  This board is completely stuffed with a full set of parts.  The TAP-28 board that will be inside the clock won't need the LEDs, the USB connector or the sockets for the daughter board, I2C/SPI or even UART, keeping the cost to a minimum.  A salvaged 5V cell-phone power supply will be hard-wired to the TAP-28 to power the clock.



This is the TAP-28 board to be included in the clock.  It has the minimum components for this application.  Other than the micro and support components, it has switches to set the time, along with their pullup resistors and connectors for ICSP, the servo and the pot if needed for future calibration.


Forum Activity

Member Access