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
The board costs under $20 and for all of the features and Micro:bit compatibility I think its a bargain
|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
import light from time import sleep_ms Right = light.Intensity(39) Left = light.Intensity(36) while True: print('Right=',Right.read()) print('Left=',Left.read()) sleep_ms(1000)
In the Shell Window you will see something like this
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.
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': display.show(Image.ARROW_E) else: display.show(Image.ARROW_W)
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