not logged in | [Login]

Fitting a VBAT battery

The real-time clock can only keep time when the PYB is powered up. However a watch battery can be easily fitted to the board to rectify this situation. CR2032 3v coin cells are readily available and a holder for these may be glued to the back of the board. I used RS part number 745-0938 (the CR2032 cells are 457-4757). The holder fits the board well and if mounted flush with the connector (top) edge of the board, all the terminal holes remain accessible. Fold the connector lugs on the battery holder back against the body and solder short solid-core wire tails to the two terminals. Solder the other end of the negative wire to the GND terminal above P8 and the positive wire to the VBAT terminal above P6.

CAUTION: With this battery holder fitted, the PYB will still fit into the plastic box it was supplied in (just), however you must remove the conductive foam inserts because these will short out the battery.

Setting the RTC

The following code prompts the user to set the RTC if necessary. The blue LED indicates the need to connect to the USB serial terminal and set the RTC.

def inputint(pr, mn, mx):
  x = input(pr)
    x = int(x)
    x = mn
  if (x < mn):
    x = mn
  if (x > mx):
    x = mx
  return x

import pyb
blue = pyb.LED(4)
dt = pyb.RTC().datetime()
if (dt[0] == 2014) and (dt[1] == 1):
    inputint("Year [yyyy]: ", 2014, 3000),
    inputint("Month [1..12]: ", 1, 12),
    inputint("Day of month [1..31]: ",1, 31),
    inputint("Day of week [1 = Monday]: ", 1, 7),
    inputint("Hour [0..23]: ", 0, 23),
    inputint("Minute [0..59]: ", 0, 59),
  dt = pyb.RTC().datetime()
print('RTC: {:04},{:02},{:02} {:02}:{:02}'.format(dt[0],dt[1],dt[2],dt[4],dt[5]))