Gator:color ProtoSnap Hookup Guide

Pages
Contributors: Englandsaurus
Favorited Favorite 0

Using MakeCode

As you've seen, it's pretty simple to get your LEDs to simply light up; just connect power to power and ground to ground. However, if you want to do some fancy dimming or flashing effects, you'll need to upload some code to your micro:bit.

If you've never used MakeCode before, refer to the Using MakeCode section of our Getting Started with the Micro:Bit.

Getting Started with the micro:bit

September 2, 2021

The BBC micro:bit is a compact, powerful programming tool that requires no software installation. Read on to learn how to use it YOUR way!

Dimming LEDs is done using Pulse Width Modulation. To use PWM in MakeCode, go to Advanced->Pins and select the analog write block, shown below.

Analog Write Block

Analog Write Block

The analog write block defaults to a value of 1023, or 100%. If you wanted to turn the light down to 50% brightness, you'd plug a value of 511 into your analog write function. Of course, you also want to make sure that whichever pin is selected in the MakeCode block is the pin that is clipped to the + pin on the LEDs. This can be done with any of the LEDs to create patterns with different brightnesses or even flashing LEDs.

Example: LED Pulse

The following example will gradually turn the LEDs up to full brightness, then gradually go back down. This will fade the LED's on and off. To do this, we write the value LightValue (Which we initialize to 0 in our on start function) to our analog pin P0 (3.3V from the LED's should be connected to this pin). We then add lightStep (which we set to 5 in our on start function) to LightValue, which changes the brightness by a tiny amount. Once LightValue reaches its maximum (1023) we multiply lightStep by -1, changing the value of lightStep to -5. This means that when we add lightStep to LightValue, LightValue will decrease. We do the same thing (multiply by -1) when LightValue reaches 0 to flip the direction around again. Finally, we add a delay of our choosing from Advanced->Control in to slow our fade down a little bit. The whole program is shown below.

Pulsing LED Code