Raspberry Pi Python RTC Interface MAX7219 Display Driver
This Raspberry Pi project uses a MAX7219 display driver under Python to produce a 8-digit LED clock. Here we learn something about Python code and handling date or time strings. It's assumed the system RTC (in my case an add on DS1307 module) is operating. Most of the Python code operates the electronics.
Python has two functions time and datetime useful for far more than a simple time delay. These functions can be formatted to output a string value representing date or time in a number of combinations. The outputs can be directly printed out or saved to a string variable. A number of examples of this are presented below.
The MAX7219 is set up for 8-digits using its internal BCD decoders. They have no code for ":" so I'll use a "-" between the numbers. The main part of the program is as follows:
initMAX7219() # set up the electronics # time returned as string # But order is reversed relative to MAX7219. str1 = time.strftime("%H:%M:%S") print str1 # Ex. 21:13:05 # print len(str1) # 8 # x is digit position must be 1 to 8 # y is used for string pointer while 1: y = 7 for x in range(1, 9): if str1[y] == ":": # output "-" writeMAX7219(10, x) y = y - 1 continue # back to if # convert y char to integer writeMAX7219(int(str1[y]), x) y = y - 1 str1 = time.strftime("%H:%M:%S")
To those of us doing Linux desktops such as Openbox or JWM "%H:%M:%S" is the format for displaying time. In this case the output is saved to a string variable.
A string is returned in str1 and is 8 characters long. An ASCII string is an array of characters where numbering begins at zero with the numbering going from left to right. So the index number would produce the following if the str1 = "21:13:05" :
str1 = "2"
str1 = "1"
str1 = ":"
str1 = "1"
str1 = "3"
str1 = ":"
str1 = "0"
str1 = "5"
The first problem besides being ASCII characters and not numbers is the order is reversed relative to the LED display on the clock. The inputs in the MAX7219 is numbered right to left then we also have to deal with the ":" character.
The problem was solved by using X as the digit position counter and Y as the string position pointer. Each iteration checks for a ":" and if present sends a the code for "-" (10) to the correct position. At the end of the for loop str1 reads the time again and the process repeats.
Raspberry Pi Clock Code in Python
Note I used a pre-made module which is labeled as above.
The python code is rpi_clock.txt
Below is more information on the time and datetime functions.
# Outputs based on your computer clock
# 12 hour format ##
# Output: 08:36:25
# Date in dd/mm/yyyy format
# Output: 21/03/2015
# Output: 20:36:25
now = time.strftime("%c")
# date and time representation
print "Current date & time " + time.strftime("%c")
# Output: Current date & time 03/21/15 20:38:34
# Only date representation
print "Current date " + time.strftime("%x")
# Output: Current date 03/21/15
# Only time representation
print "Current time " + time.strftime("%X")
# Output: Current time 20:38:34
# Display current date and time from now variable
print ("Current time %s" % now )
# Output: Current time 03/21/15 20:38:34
Now we use a library datetime. The syntax is:
now = datetime.datetime.now()
j = datetime.datetime.now()
print ("dd/mm/yyyy format = %s/%s/%s" % (j.day, j.month, j.year) )
print ("Current date & time = %s" % i)
print ("Current second = %s" %j.second)
print ("Current minute = %s" %j.minute)
print ("Current hour = %s" %j.hour)
print ("Current date (day) = %s" %j.day)
print ("Current month = %s" %j.month)
print ("Current year = %s" %j.year)
print ("hh:mm:ss format = %s:%s:%s" % (j.hour, j.month, j.second) )
I got the following outputs:
dd/mm/yyyy format = 21/3/2015
Current date & time = 2015-03-21 20:58:26.656000
Current second = 26
Current minute = 58
Current hour = 20
Current date (day) = 21
Current month = 3
Current year = 2015
hh:mm:ss format = 20:3:26
Additional codes for date and datetime and Linux in general. Feel free to experiment.
%a Weekday name.
%A Full weekday name.
%b Abbreviated month name.
%B Full month name.
%c Appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Equivalent of either AM or PM.
%S Second as a decimal number [00,61].
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53].
All days in a new year preceding the first Sunday are considered to be in week 0.
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first
day of the week) as a decimal number [00,53].
All days in a new year preceding the first Monday are considered to be in week 0.
%x Appropriate date representation.
%X Appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%Z Time zone name (no characters if no time zone exists).
%% A literal '%' character.
- LM334 CCS Circuits with Thermistors, Photocells
- Photodiode Circuits Operation and Uses
- Photodiode Op-Amp Circuits Tutorial
- Electronics hacks:
- Connecting PCF8574P GPIO Expander to Raspberry Pi
- Programming PCF8574P 8-bit I-O Expander with Arduino
- Connect-Program Raspberry Pi and a MM5451 LED Display Driver
- Raspberry Pi Python RTC with MAX7219 Display Driver
- Raspberry Pi and the 8-Digit LED MAX7219 Display Driver
- Programming Raspberry Pi and the 74HC595 Serial Shift Register
- Interface I2C LCD to Raspberry Pi in C
- ADS1115 4-Channel ADC Uses I2C with Raspberry Pi
- MCP4725 12-Bit DAC Interface to Raspberry Pi
- WiringPi and Pulse-Width-Modulation with Raspberry Pi
- WiringPi for Raspberry Pi and MAX6675 thermal-couple sensor
- WiringPi Blink an LED Demo
- Simple GPIO Reference Box
- Raspberry Pi with PCF8591 Analog To Digital Control in C
- Raspberry Pi PCF8591 AD-DA Sensor Python Interface
- Digital Circuits:
- Simple Schmitt Trigger SN74HC14 Square Wave Generator
- Introduction to RC Differentiator Circuits and Uses
- SN74HC14 Square Wave Generator uses SN7476 JK Flip-Flop
- Three Output Pulse Generator Circuit for Digital Circuits
- Astable CD4047 Geiger Counter Power Supply
- CD4047 Monostable Multivibrator Circuit
- Basic TTL Tri-State Buffer Circuit Examples
- Tutorial NOR Gate SR Latch Circuits
- Tutorial NAND Gate SR Latch Circuit
- Tutorial OR-NOR Circuits Including Monostable Multivibrator
- Brief Tutorial of XOR and XNOR Logic Gates
- LM555-NE555 One-Shot Multivibrator AC Power Control
- Arduino PWM to Analog Conversion
- Arduino Analog Digital Conversion Voltmeter
- Better Arduino Rotary Encoder Sensor
- Simple 3-Wire MAX6675 Thermocouple ADC Arduino Interface
Most Popular videos and Webpages
- Hall Effect Magnetic Switches and Sensors
- Basic Hall Effect Sensors YouTube
- Opto-Isolated Transistor Drivers for Micro-Controllers
- Opto-Couplers Theory and Circuits YouTube
- H-Bridge Motor Control with Power MOSFETS Updated
- Build Power MOSFET H-Bridge for Arduino YouTube
- LM317 High Power Constant Current Source Circuit
- Adjustable LM317 High Power Current Source
- ULN2003A Darlington Transistor Array with Circuit Examples
- ULN2003A Transistor Array with Arduino
- Constant Current Circuits with the LM334, LM317
- Constant Current Source Tutorial YouTube
- N-Channel Power MOSFET Switching Tutorial
- P-Channel Power MOSFET Switch Tutorial
- Using Power MOSFETs with Arduino YouTube
- Zero-Crossing Detectors Circuits and Applications
- Zero-Crossing Circuits for AC Power Control
- In Depth Look at AC Power Control with Arduino
- Micro-controller AC Power Control Using Interrupts
- YouTube Video for Arduino AC Power Control
- All NPN Transistor H-Bridge Motor Control
- All NPN Transistor H-Bridge Motor Control YouTube