Spring Sale: 5% OFF Over \$89, Code: Elephant5; 10% OFF Over \$149, Code:Elephant10

# Microbit Python Tutorials 11: Colorful water lights

Posted by SuFiona on

Course11-Colorful water lights

Learning goals:

This lesson learns to use Python programming to light up the water lights of micro:bit smart car.

Code

from microbit import *

import neopixel

display.show(Image.HAPPY)

# The water lamp is connected to pin pin16, the number is 3

np = neopixel.NeoPixel(pin16, 3)

iterate each LED in the water lights

for pixel_id in range(0, len(np)):

# Light up the first water light to red

np[0] = (255, 0, 0)

# display color

np.show()

In the program, import neopixel is means to import neopixel library, we can make micro:bit robot display a smile on the lattice. Then define the pin of the water light as pin16, the number is 3, iterate each LED in the water lights., np[0] = (255, 0, 0) means that the first water light is red. Modify the parameters in the brackets to change the color of the light.

1.You should open the Mu software, and enter the code in the edit window, , as shown in Figure 11-1.

Figure11-1

2.As shown in Figure 11-2, you need to click the Check button to check if our code has an error. If a line appears with a cursor or an underscore, the program indicating this line is wrong.

Figure 11-2

3.You need to connect the micro data cable to micro:bit and the computer, then click the Flash button to download the program to micro:bit as shown in Figure 11-3.

Figure 11-3

4. The schematic diagram of the flow lamp of the robot is shown in Figure 11-4. As you can see, the flow light of the robot is connected to the pin16 of the micro:bit. Therefore, we set the pin of the flow lamp to pin16 in the program. After downloading the program to micro:bit, you can see a smile on the dot matrix of the robot as shown in Figure 11-5, and light the first water light to red.

Figure 11-4

Figure 11-5

The code of the experiment：11.Colorful water lights.rar