micropython___circuitpython_suganth-466917-unsplash.jpg
Photo by Suganth on Unsplash

Quick Reference

Delays

Arduino

delay(1500);

CircuitPython

import time
time.sleep(1.5)

System Time

Arduino

unsigned long now = millis();

CircuitPython

import time
now = time.monotonic()

Discussion

Being able to wait a specific amount of time is important in many projects. 

The methods Arduino and CircuitPython handle this are very similar.

Delays

Arduino

The function used to wait a specific time, delay, is built into the Arduino framework. No #include is required. The single argument is the time to delay in milliseconds.

An example is the simple blink LED code shown below:

Download: file
int ledPin = 13;                 // LED connected to digital pin 13

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}

The delay function waits one second (1000 milliseconds) each time the ledPin is toggled on or off to create a blinking light.

CircuitPython

The equivalent function to delay in CircuitPython is the time.sleep function. The time module is not included by default, it must be imported into the program.

The argument passed into time.sleep is the time to delay in seconds (not milliseconds). The value can be a decimal so if you wanted to wait one second you'd put in 1.0, if you wanted 50 milliseconds, it would be 0.050.

The code below prints the word Hello repeatedly. The prints are spaced by a delay of 0.1 seconds, which is 100 milliseconds.

Download: file
import time

while True:
    print("Hello")
    time.sleep(0.1)
micropython___circuitpython_djim-loic-69263-unsplash.jpg
Photo by Djim Loic on Unsplash

Get System Time

In general, microcontrollers and single board computers often lack a Real-Time Clock (RTC) module to know what the time is. What each system generally does have is a count of the time since it was powered on. This is still useful to do certain work that spans a period of time.

Arduino

To get the time since a board has been on in Arduino, the common function is millis. It has no arguments and returns the number of milliseconds since the board was last powered on.

The following code sketch prints the time since power on to the serial port every second (1000 milliseconds).

Download: file
unsigned long initial;

void setup(){
   Serial.begin(9600);
   initial = millis();
}

void loop(){
   unsigned long now;
   now = millis();
   if( now - initial > 3 ) { // Print done after 3 milliseconds elapses
      Serial.print("done");
   }
   else {
      initial = now;
   }
}

CircuitPython

Python has a number of time functions. But many boards suffer the same issue as Arduino finds - there is no RTC. 

CircuitPython has a similar function to the Arduino delay called time.monotonic. But it returns seconds, not milliseconds like delay.

Note, it is not recommended to compare times greater than an hour or so as the value will start rounding and could lose time and thus accuracy at that point.

Download: file
import time

initial = time.monotonic()  # Time in seconds since power on

while True:
    now = time.monotonic()
    if now - initial > 0.003:  # If 3 milliseconds elapses
        print("done")
    else:
        initial = now

Reference

Arduino

Arduino Reference

CircuitPython

This guide was first published on Oct 22, 2018. It was last updated on Oct 22, 2018. This page (Time) was last updated on Oct 22, 2019.