Your Bit:Bot has 2 tiny light sensors on the front of either side. We will use these sensors in a later project to create a light avoiding and light seeking robot but before we do that we need to have a look at how these sensors work.
The Micro:Bit has 2 types of input/output pins digital and analogue; the digital pins are either a 1 or a 0, on or off and the analogue pins can have 1024 different values 0 – 1023. These values are represented by a 10 bit binary number in the Micro:Bits memory. The light sensors will alter the voltage going to some of the analogue pins depending on how much light they’re detecting; the more light, the greater the voltage and the greater the number detected by the analogue pins.
In this example we will take a reading from the light sensors and alter the brightness of the LEDs depending on the brightness of the detected light from the light sensors.
Open Mu and click the new button to start a new project. Then open the last project we did with the NeoPixels and copy and paste it into your new project. Then delete everything below the program loop. That will be everything below the while True: statement. Once that’s done delete the comments at the top of the screen and write something more appropriate. Or if that sounds like hard work just copy and paste the code below into your new project.
# Light sensor example for the 4tronix Bit:Bot and BBC Micro:Bit # Author David Bradshaw 2017 # Demonstrates how to use the light sensors from microbit import * import neopixel # Neopixel Library so we can control the NeoPixels lights np = neopixel.NeoPixel(pin13, 12) def leftLights(Red, Green, Blue): for pixel_id in range(0, 6): np[pixel_id] = (Red, Green, Blue) np.show() def rightLights(Red, Green, Blue): for pixel_id in range(6, 12): np[pixel_id] = (Red, Green, Blue) np.show() while True:
Now save the new project with a file name of your choice. Be careful not to overwrite the old NeoPixel example. If you do and require the code at a later date you can always go to the Resources page and download the code you need.
Now we will assign the analogue pins that the light detectors are connected to. You can have a look at the bottom of your Bit:Bot to see what these pins are. Type the below code underneath the line np = neopixel.NeoPixel(pin13, 12)
# Both light sensor are on the same pin so we also use a select pin lightSensor = pin2 sensorSelect = pin16
We have assigned pin 2 for the light sensors and pin 16 for sensor select. When 4Tronix deigned Bit:Bot they decided to connect both sensors to the same pin and have another pin as a pin select. Although we have only assigned one pin for the light sensor we have 2 light sensors connected to that pin.
Now we’re going to create a method that will read the values from each light sensor and set the brightness of the NeoPixels depending on the brightness of the detected light. under the rightLights() method make sure that there’s 2 blank lines with no white space and type;
def lightSense(): sensorSelect.write_digital(0) brightness = lightSensor.read_analog() brightness = int(brightness / 100) leftLights(brightness, 0, 0) sensorSelect.write_digital(1) brightness = lightSensor.read_analog() brightness = int(brightness / 100) rightLights(brightness, 0, 0) while True: lightSense()
I’ve introduced a few new things in the above code so I will explain what it is doing in plain English.
- We select sensor 0 which is the left sensor
- We then assign sensor 0’s value (via the read_analog() function) to an integer called brightness
- We then divide this number by 100 and ensure that the result is an integer value
- we then assign that value to the left LEDs (red colour component only) on Bit:Bot via the method leftLights()
- Above steps are then repeated for the right sensor
Here we use write_digital() to write a value to a digital pin and read_analog() to read an analogue value from the sensors. We then divided this number by 100 and use a technique called casting to ensure that the resulting value is an integer.
Integers are whole numbers i.e 1, 10, 22, 1547, etc anything that is not a whole number i.e. 1.24, 5.5, 154.25 can not be stored in an integer variable, if we didn’t cast the value to an int Bit:Bot would have given us an error. This is because we’re dividing brightness by a number; the result of which could be a decimal number that can’t be stored as an integer value. By casting what we have done is to round up this number to the nearest whole number so it can be stored as an integer value (and not crash the Micro:Bit). How decimal numbers or floating point numbers as we call them in the computing world are stored is a subject for another day. If your interested in this I suggest you look into data types, binary number system, floating point numbers and 2’s complement.
Maybe your wondering why I even divided the brightness in the first place; this is because the maximum number you can give to the method leftLights() is 255. This is due to it using an 8 bit binary number to store each colour components brightness. 2^8 is 256 which gives us the number range of 0 – 255. If we gave that method a number larger than 255 we would get an overflow error and the Micro:Bit would crash. The analogue pins have a precision of 2^10 this means that it can have 1024 different values 0 – 1023. In order to avoid the overflow error we need to divide brightness by at least 4 in order to ensure that it will always be below 256. I decided to divide it by 100 to keep the brightness of the LEDs down so it doesn’t hurt your eyes or give you a headache 🙂 .
The completed code for this exercise is below;
# Light sensor example for the 4tronix Bit:Bot and BBC Micro:Bit # Author David Bradshaw 2017 # Demonstrates how to use the light sensors from microbit import * import neopixel # Neopixel Library so we can control the NeoPixels lights np = neopixel.NeoPixel(pin13, 12) # Both light sensor are on the same pin so we also use a select pin lightSensor = pin2 sensorSelect = pin16 def leftLights(Red, Green, Blue): for pixel_id in range(0, 6): np[pixel_id] = (Red, Green, Blue) np.show() def rightLights(Red, Green, Blue): for pixel_id in range(6, 12): np[pixel_id] = (Red, Green, Blue) np.show() def lightSense(): sensorSelect.write_digital(0) brightness = lightSensor.read_analog() brightness = int(brightness / 100) leftLights(brightness, 0, 0) sensorSelect.write_digital(1) brightness = lightSensor.read_analog() brightness = int(brightness / 100) rightLights(brightness, 0, 0) while True: lightSense()
Check your code with the check button and Flash it to your Micro:Bit. Cover up the sensors and watch the LEDs get brighter and dimmer as you do it. The sensors are on the ends of the arms where the LEDs are.
Now we’re going to change the lightSense() method so it will return a brightness value rather than setting the brightness. It will also allow us to set the minimum brightness.
def setBrightness(minValue): sensorSelect.write_digital(0) brightnessLeft = lightSensor.read_analog() sensorSelect.write_digital(1) brightnessRight = lightSensor.read_analog() brightness = int((brightnessLeft + brightnessRight) / 2) brightness = int(brightness / 25) if(brightness < minValue): brightness = minValue return brightness
The above setBrightness() method returns a value depending on ambient light levels. This method will be used in the next example; I have used if statements and return values don’t worry about them we will cover it all in the next example when we play with the IR detectors. Look at the code and try to figure out what its doing, what’s the if statement for and why do I add both brightness values together and divide by 2? put your thoughts in the comments section.
This code doesn’t do anything exciting however we have covered some important points. Building on what we went through in the previous exercise then reading from an analogue pin, writing to a digital pin, assigning pins and casting to ensure that results of a calculation is in a usable format. The setBrightness() method will be used for all examples that utilise the LEDs to ensure that they are not too bright.
Well done for completing this example we can now light up the LEDs and use the light sensors to detect light. When you feel confident to move on go to the next tutorial that will look at how to use the IR detectors on the bottom of the Bit:Bot to detect black lines so we can create a line following robot.
The below zip file contains the above code and a few extra bits
Please feel free to comment below or upload code related to this example.