Home bpi:bit bpi:bit and light sensors micropython example

bpi:bit and light sensors micropython example

by shedboy71

In this example we will look at the 2 light sensors that are fitted to the bpi:bit. We will also see how you to use Micropython to return a reading from them and also a basic gesture detection example


As stated there are 2 sensors, they use the following pins

Photosensitive sensor: GPIO36 (Analog A0, upper left), GPIO39 (Analog A3, upper right)
The sensor is a PTSMD021, which appears to be a Chinese made sensor. Not a lot of documentation other than a datasheet in Chinese but these are fairly basic sensors to use.

Parts Required

The board costs under $20 and for all of the features and Micro:bit compatibility I think its a bargain

Name Link
bpi:bit Banana PI Bit board with EPS32 for STEAM education




Basic Code Example

I used Thonny for development

You need to import the Light module, as stated the light sensors are on GPIO 36 and 39 so we need to access these pins and use the read function to get the value.

We run this continously in a while loop but there is a 1000 ms delay between readings

[codesyntax lang=”python”]

import light
from time import sleep_ms

Right = light.Intensity(39)
Left = light.Intensity(36)

while True:




In the Shell Window you will see something like this

Right= 85
Left= 67
Right= 85
Left= 66
Right= 84
Left= 66
Right= 97
Left= 0
Right= 96
Left= 0
Right= 0
Left= 27
Right= 0
Left= 27
Right= 101

Gesture Code Example

I used Thonny for development for this, using the example

  • First of all we need to import the light module, and then instantiate the Gesture() class, ts = light.gesture (), through which the Gesture detection is initialized
  • Then you call the get_gesture () method to test the action, the detection function requires about 25 ms time.
  • The get_gesture() method returns ‘right’ if it detects a right gesture, and ‘left’ if it is a left gesture.

[codesyntax lang=”python”]

import light
from display import*

ts = light.Gesture()
display = Display()
t = 0

while True:
   res = ts.get_gesture()
   if res != None:
       t = t+1
       print(res, t)
       if res == 'right':



Open the shell and wave your hand over the bpi:bit, you should see a left or right arrow on the bpi:bit 25 led display and in the shell see something like this.

I have observed you need to do this in good light to work really well, my original attempts in a dim room did not seem to register the gestures reliably

left 1
right 2
right 3
right 4
right 5
right 6

You may also like

Leave a Comment