Example Code for Arduino-Measure Light Source Flicker Frequency
Last revision 2026/01/15
Measure whether the flicker frequency of ambient light is 50Hz or 60Hz. Example Code 2.1 and code 2.2 can be used to simulate the flicker of ambient light at 50Hz or 60Hz.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- Gravity: AS7341 Visible Light Sensor x1
- Wires
- Another main-controller x1
- LED x1
Software Preparation
- Arduino IDE
- Download and install the AS7341 library and Sample Code. (About how to install the library?)
Wiring Diagram
Connect an LED to the Digital pin 10 of another main-controller.
Other Preparation Work
Burn the codes to another main-controller for simulating 50Hz or 60Hz light flicker.
Sample Code
Sample Code 2- Measure light source flicker frequency
/*!
* @file getFlicker.ino
* @brief Read the flicker frequency of light source
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [fengli]([email protected])
* @version V1.0
* @date 2020-07-16
* @get from https://www.dfrobot.com
* @url https://github.com/DFRobot/DFRobot_AS7341
*/
#include "DFRobot_AS7341.h"
/*!
* @brief Construct the function
* @param pWire IC bus pointer object and construction device, can both pass or not pass parameters, Wire in default.
*/
DFRobot_AS7341 as7341;
void setup(void)
{
Serial.begin(115200);
//Detect if IIC can communicate properly
while (as7341.begin() != 0) {
Serial.println("IIC init failed, please check if the wire connection is correct");
delay(1000);
}
}
void loop(void){
uint8_t freq = 0;
//Read the value of register flicker, through which the flicker frequency of the light source can be predicted
freq = as7341.readFlickerData();
if (freq == 1) {
Serial.println("Unknown frequency");
} else if (freq == 0) {
Serial.println("No flicker");
} else {
Serial.print(freq);
Serial.println("Hz");
}
}
Sample Code 2.1 - Simulate 50Hz light flicker
//50HZ
//Burn the codes to another main-controller, and connect an LED onto the Digital pin 10 to provide 50Hz ambient light for getFlicker.ino.
void setup() {
pinMode(10, OUTPUT);
}
void loop() {
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
delay(5);
digitalWrite(10, LOW); // turn the LED on (HIGH is the voltage level)
delay(5);
}
Sample Code 2.2 - Simulate 60Hz light flicker
//60HZ
//Burn the codes to another main-controller, and connect an LED onto the Digital pin 10 to provide 60Hz ambient light for getFlicker.ino.
void setup() {
pinMode(10, OUTPUT);
}
void loop() {
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
delayMicroseconds(4167);
digitalWrite(10, LOW); // turn the LED on (HIGH is the voltage level)
delayMicroseconds(4167);
}
Result

Additional Information
Burn the codes to another main-controller, and connect an LED to the Digital pin 10 to simulate the light source of 50Hz or 60Hz.
Was this article helpful?
