Download Print this page

Sparkfun Electronics Inventor's Kit Manual page 25

For micro:bit

Advertisement

Running Your Script
Either copy and paste, or re-create the following code into your own MakeCode editor by clicking the
open icon in the upper right-hand corner of the editor. You can also just download this example by
clicking the download button in the lower right-hand corner of the code window.
Note: You may need to disable your ad/pop-up blocker to interact with the MakeCode programming
environment and simulated circuit!
Code to Note
Let's take a look at the code blocks in this experiment.
If you are having a hard time viewing this code, click on the image above to get a better look!
A "variable" is a placeholder for values that may change in your code. You can create a variable
using th
option underneath the Variables group. You can then name it, which then
Make Variable
creates a block for your given variable.
Set To
To store a value inside of your newly created variable you use the
block. The
block
set to
set to
allows you to select from a list of the variables that exist in your program and then add a value that
you want to store or set that variables to.
Analog Read
In this program you are reading the voltage from the potentiometer which is 0 to 3.3 volts. The
micro:bit reads that value as a 10 bit number which is a value range from 0 to 1023 using the
analog 
block. The
block is a value based block, meaning that you have to insert it into a
read
analog read
block with a matching shape. We insert it into the
block to store its value as a variable.
set to
Analog Write
Just like the
block the
block deals with a range of values, but instead of
analog write
analog write
reading a pin as an input the
block outputs an analog value to a pin. We see this as a
analog write
brightness range with this led, but it could be a tone from a buzzer, a motor speed, etc. We set our
analog output to the variable we stored the potentiometer value in.
What You Should See
You should twist the potentiometer. You will notice that the LED will get brighter or dimmer based on
the position of the potentiometer. If you turn the potentiometer all the way one direction it will be fully
on and the other end will be fully off.

Advertisement

loading
Need help?

Need help?

Do you have a question about the Inventor's Kit and is the answer not in the manual?

Subscribe to Our Youtube Channel