7 Segment Display Internet Clock


Download 7 Segment Display Internet Clock


Preview text

7 Segment Display Internet Clock
Created by Philip Moyer

https://learn.adafruit.com/7-segment-display-internet-clock
Last updated on 2021-11-15 06:41:14 PM EST

©Adafruit Industries

Page 1 of 15

Table of Contents

Objective

3

What You'll Need

4

Soldering (aka Build It!)

4

Code

7

• FeatherClock Code

8

Run it!

13

©Adafruit Industries

Page 2 of 15

Objective
When I saw these 7 segment displays with FeatherWing backpacks on Ask an Engineer, I got really excited. I love clocks! I have the original Ice Tube clock from Adafruit, plus the PiGlow daughter card binary clock, plus my collection of watches (including a programmable TI digital watch that I've re-programmed to display Martian Standard Time and the current Sol). So the thought of having a mobile clock with a colored display made me immediately run to the shop and buy three colors (and a Circuit Playground, but that's for another tutorial). This clock design uses NTP (Network Time Protocol) to always display the accurate time, synchronized to the same clocks that run the Internet!

©Adafruit Industries

Page 3 of 15

Follow along to build your own ultra-precision clock that always has the exact right time, synced to Internet atomic-accuracy clocks!
What You'll Need
I'm not going to include my usual architecture diagram because this device is pure simplicity. It's just a Feather Doubler with a 7 segment LED FeatherWing (https:// adafru.it/Cgm) (in the color of your choice), along with a Feather M0 WiFi (http:// adafru.it/3010)with an OLED FeatherWing on top (http://adafru.it/2900) (for debugging output). You can get all the parts you need from the shop - see the list at the right. You'll also need a soldering station for the light soldering needed to put the headers on the boards and the LED block onto the Backpack.
Here are the components after soldering but before assembly:

Soldering (aka Build It!)
As with all Feathers and Wings, you have a choice of headers to solder on. I recommend the Feather Stacking headers for the Feather M0 WiFi itself, the included male header strips for the 7 segment backpack FeatherWing and OLED FeatherWing, and the Feather Header Kit for the Doubler (the Doubler comes with one pair of stacking headers and one pair of normal headers; I prefer the normal headers, so I always order an extra Feather Header Kit with each Doubler I get).

©Adafruit Industries

Page 4 of 15

This header arrangement maximizes the visibility of the LED display (and I don't think other headers will fit along with the LED display anyway).
If you are new to soldering, read one of the excellent guides to perfect soldering (htt ps://adafru.it/dxy).
First, solder the stacking headers onto the Feather M0 WiFi. Put the male pins below the board and the female header above the board. Tack down one pin first, then eyeball it to make sure the headers are at 90 degree angles to the board. When you're satisfied with the alignment, solder the rest of the pins. Repeat with the other piece of stacking header.
Second, solder the headers you like onto the Doubler. Be careful to be sure they are at a 90 degree angle to the Doubler. If you want, you can put the Feather into the headers when you solder them in to assist with the alignment. This might make soldering them in straight a little easier.
After you have the Doubler constructed, put the Feather M0 WiFi in it. Make sure the JST connector for the battery is on the outside edge.

©Adafruit Industries

Page 5 of 15

Third, solder the male header strips onto the 7 Segment Backpack FeatherWing. The LED outlines should be on the top of the board. You'll need to shorten one of the header strips (I use a pair of needlenose pliers and snap off the header to the correct length) to fit in the 12-pin side of the FeatherWing. Fourth, carefully insert the pins on the 7 segment display into the FeatherWing. This may require a bit of patience, but work slowly and it will eventually slide right in. You want to avoid bending the pins on the display while doing this. Flip the FeatherWing over and carefully solder the display pins on the bottom side of the board. Be careful not to heat the IC or its pins too much.
Put the 7 segment FeatherWing on the other side of the Doubler.

©Adafruit Industries

Page 6 of 15

Fifth, solder the headers you like onto the OLED FeatherWing. I prefer the included male headers for this because I'm never going to stack anything on top of the OLED display. Put the OLED FeatherWing on top of the Feather M0 WiFi.
That's it! That's all there is to the soldering.
Finish assembling the clock. Plug in the battery (if you want to use one; you can power it from USB if you like). I have enough LiPos lying about that I went for the disconnected and mobile approach with the battery.

Next: the Code
Code
Before you start, go through the following guides:
• Feather M0 Wifi Guide (https://adafru.it/ne7) • OLED Featherwing Guide (https://adafru.it/nek) • 7-segment Featherwing Guide (https://adafru.it/nel)
To make sure you have installed all the drivers, Arduino libraries, and examples. Make sure you have code uploading and the examples running before you continue to this combined code example!

©Adafruit Industries

Page 7 of 15

FeatherClock Code
I based my code on examples from the libraries - namely the WiFiUdpNtpClient example from the WiFi101 library and the clock_sevenseg_ds1307 example from the LED Backpack Library. I modified both to meet the needs of this project.
You will need to modify three things in the below code to make the clock work.
• First, change YOUR_SSID to the SSID for your wireless network. • Second, change YOUR_WIFI_PASSWORD to the actual password for your SSID
network. • Third, change the tzOffset variable to be the correct offset from UTC for your
timezone. The code below is configured for the East Coast of the US (Eastern Standard Time - tzOffset = -4). If you'd like your clock to display UTC, simply set the tzOffset to zero.

//########################################################################## // FeatherClock - Use the Adafruit seven segment LED FeatherWing to display // the current time, which we fetch from the Internet using NTP. This // program assumes the seven segment display is attached to a Feather M0 // WiFi (WINC1500). // // At the top of every hour, re-sync with NTP to correct for any drift. // // Credit: This is based heavily on the work by Tony DiCola from Adafruit. // Suggest (strongly) you go to adafruit.com and buy some product from // them. WINC1500 code modified from example code provided by Michael // Margolis and Tom Igoe. // // Philip R. Moyer // Adafruit // // This source code is released under the BSD license. // Any further redistribution must include this header. //##########################################################################

//########################################################################## // Includes and defines //##########################################################################

#include #include #include #include #include #include #include

<SPI.h> <Wire.h> <WiFi101.h> <WiFiUdp.h> <Adafruit_LEDBackpack.h> <Adafruit_GFX.h> <Adafruit_SSD1306.h>

// SPI interface API // Wire support library // Wireless header // UDP support // Support for the Backpack FeatherWing // Adafruit's graphics library // OLED support library

#define WINC_CS

8

#define WINC_IRQ

7

#define WINC_RST

4

#define WINC_EN

2

// WINC1500 Chip Select // WINC1500 interrupt request line // WINC1500 reset line // WINC1500 enable

#define OLED_RESET

3

// OLED reset pin

©Adafruit Industries

Page 8 of 15

#define VBATPIN

9

// Pin to read battery voltage

//########################################################################## // Globals //##########################################################################

#define TIME_24_HOUR

true

#define DISPLAY_ADDRESS 0x70

// Create display object Adafruit_7segment clockDisplay = Adafruit_7segment();

int hours = 0; int minutes = 0; int seconds = 0; int tzOffset = -4;

// Track hours // Track minutes // Track seconds // Time zone offset (-4 = US Eastern time)

bool blinkColon = false;

// Track colon status to blink once/sec

int status = WL_IDLE_STATUS;

// WINC1500 chip status

char ssid[] = "YOUR_SSID";

// WiFi network SSID

char pass[] = "YOUR_WIFI_PASSWORD"; // WiFi password (NOTE: redact before doing a

git push!!!)

unsigned int localPort = 2390;

// Local port to listen for UDP packets

IPAddress timeServer(129,6,15,28); // time.nist.gov NTP server

const int NTP_PACKET_SIZE = 48;

// NTP time stamp is in the first 48 bytes

byte packetBuffer[NTP_PACKET_SIZE]; // Buffer for incoming and outgoing UDP packets

WiFiUDP Udp;

// Set up a WINC1500 client session

Adafruit_SSD1306 display(128, 32, &Wire, OLED_RESET); // Instantiate OLED object

//########################################################################## // Functions //##########################################################################

// send an NTP request to the time server at the given address

unsigned long sendNTPpacket(IPAddress& address)

{

//Serial.println("1");

// set all bytes in the buffer to 0

memset(packetBuffer, 0, NTP_PACKET_SIZE);

// Initialize values needed to form NTP request

// (see URL above for details on the packets)

//Serial.println("2");

packetBuffer[0] = 0b11100011; // LI, Version, Mode

packetBuffer[1] = 0;

// Stratum, or type of clock

packetBuffer[2] = 6;

// Polling Interval

packetBuffer[3] = 0xEC; // Peer Clock Precision

// 8 bytes of zero for Root Delay & Root Dispersion

packetBuffer[12] = 49;

packetBuffer[13] = 0x4E;

packetBuffer[14] = 49;

packetBuffer[15] = 52;

//Serial.println("3");

// all NTP fields have been given values, now // you can send a packet requesting a timestamp: Udp.beginPacket(address, 123); //NTP requests are to port 123 //Serial.println("4"); Udp.write(packetBuffer, NTP_PACKET_SIZE); //Serial.println("5"); Udp.endPacket(); //Serial.println("6"); }

void printWiFiStatus() {

©Adafruit Industries

Page 9 of 15

// print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID());

// print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip);

// print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm"); }

void printData() { float measuredvbat = analogRead(VBATPIN);

measuredvbat *= 2; measuredvbat *= 3.3; measuredvbat /= 1024;

// we divided by 2, so multiply back // Multiply by 3.3V, our reference voltage // convert to voltage

display.clearDisplay(); display.setCursor(0,0); display.print("VBat: "); display.print(measuredvbat); display.println(" volts"); display.display(); }

//########################################################################## // Setup - main //##########################################################################

// This is one of the two standard functions for every Arduino program, // with the other being loop(). This one runs once at the beginning of the // program execution and is ordinarily used to initialize hardware and // variables.

void setup() { #ifdef WINC_EN
pinMode(WINC_EN, OUTPUT); digitalWrite(WINC_EN, HIGH); #endif

WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);

Serial.begin(115200); Serial.println("Clock starting!");

// Start the serial console // Start the clock message.

// Make sure we have WiFi hardware, or there's no point continuing.

if (WiFi.status() == WL_NO_SHIELD) {

while (true);

// Spin loop

}

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32

// Show initial display buffer contents on the screen -// the library initializes this with an Adafruit splash screen. display.display(); delay(2000); // Pause for 2 seconds

// Clear the buffer. display.clearDisplay();

display.setTextSize(1); display.setTextColor(WHITE);

©Adafruit Industries

Page 10 of 15

Preparing to load PDF file. please wait...

0 of 0
100%
7 Segment Display Internet Clock