Skip to main content

Sugar - Photosensitive


Detect the ambient light intensity and return the analog data. image.png


Dimension24 x 24 x 16 mm
Weight4.7 g
SignalAnalog input
Range0~3.3V correspond to future board 0~4095

Using on micro:bit


Coding Platform

Microsoft MakeCode for micro:bit Use Makecode as coding platform

Add Sugar Plugin

image-20240311154521941 image.png Search Sugar in extensions, click add

Building Blocks - Function Description

Serial NumberBuilding Block ImageBuilding Block Function
1image-20240315174137978Get the numerical value of the light-sensitive sensor (0-1023)

Circuit Connection


Microbit InterfaceWiring
Light Sensor.pngLight SensorP0White PH2.0-3Pin Interface Wireuntitled.100.png


Case: Determine Light Intensity


Using Kittenblock

Connect the light sensor to the P1 of Robotbit Edu using a 3PIN terminal wire. Turn on the Robotbit power supply to see the red light at the bottom of the light sensor module light up (indicating that the module is powered normally) light.png

The brightness of the future board screen is filled with the screen, and the filled value is the lightness value 0~255(from black to white). If the ambient light value 0~4095 is to correspond to the screen brightness value 0~255, the mapping block needs to be used to correspond the two intervals one by one. Choose to run the program in the online running or offline uploading mode to see the effect


If you are not sure how to run online or upload offline, please refer to Quick Start


Ambient Light Value


  • Return value: 0~4095
from future import *
from sugar import *
light = Light('P1')

Interval mapping formula, no need to understand too much, just use it directly

def valmap(x, in_min, in_max, out_min, out_max): return int((x-in_min) * (out_max-out_min) / (in_max-in_min) + out_min)

Turn off synchronous refresh for smoother screen

screen.sync = 0 while True: x = light.value() screen.fill(round(valmap(x, 0, 4096, 0, 255))) screen.refresh()

Effect Demonstration

Realize the brightness of the ambient light, and the brightness of the face board screen will change accordingly. light.mp4