Example Code for micro:bit-MakeCode Programming
This project demonstrates how to use MakeCode programming to read ultraviolet (UV) data from the Gravity: 240370 Ultraviolet Index Sensor with a micro:bit. Users will learn to add the UV sensor library to MakeCode, create a program, and upload it to the micro:bit.
Hardware Preparation
- micro:bit x1
- Gravity: 240370 Ultraviolet Index Sensor x1
Software Preparation
- MakeCode programming platform: https://makecode.microbit.org/
Wiring Diagram

Other Preparation Work
- Open the MakeCode programming platform by typing the following URL into the browser: https://makecode.microbit.org/
- Create a new project and name it.
- Load the UV sensor's program library: Click "Settings", "Extensions" in order, then paste the following link into the search box: https://github.com/cdjq/pxt-DFRobot_UVIndex240370Sensor
Sample Code
Start programming, the program is as shown in the following figure:

Result
Click download, after the program is uploaded, open "Show data device" to see the data output.
Was this article helpful?
