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:
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.
import time while True: print("Hello") time.sleep(0.1)
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).
unsigned long now; void setup(){ Serial.begin(9600); now = millis(); } void loop(){ if((now + 1000) < millis()) { // Print done after 1000 milliseconds (aka 1 second) elapses Serial.println("done"); now = millis(); } }
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 millis
called time.monotonic
. But it returns seconds, not milliseconds like millis
.
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.
import time now = time.monotonic() # Time in seconds since power on while True: if (now + 0.003) < time.monotonic(): # If 3 milliseconds elapses print("done") now = time.monotonic()
Reference
Arduino
Arduino Reference
CircuitPython
Page last edited March 08, 2024
Text editor powered by tinymce.