1 Output  Input 

Output

Logic should dictate that input comes before output but the children find it more motivating to begin by making something happen. Output has the added benefit that the scripts are simpler to create and understand.

The micro:bit has a number of built in sensors that can be used for data input as well as two 'push to make' buttons labelled A and B. 

The row of brass tabs along the bottom edge of the micro:bit are for connecting electrical components and circuits to the micro:bit so that it can read data from electrical components or write data (in the form of an electrical voltage) to them.

There are 5 big tabs with 4 mm terminals and 20 smaller tabs. When coding, these tabs are referred to as 'pins'.

(see Connecting the LED below)

Task:

Light an LED connected to pin 0

The model:

Connections can be made with crocodile clips, micro:pegs, 4 mm 'banana' plugs, 4 mm spring connectors or crocodile clips. 

The most common method of making connections to the edge strip is using short, 30 cm, leads with an insulated crocodile clip at each end.

If you have access to a 3D printer I strongly recommend that you use micro:pegs to make connections - follow the link below to find out more and access a .stl file to print.


The picture below shows an LED with the positive side (red wire) being connected to pin 0 and the negative side (black wire) connected to GND.  

See the Practical tips page for advice  on making electrical circuits.


The LED in the picture is connected to the micro:bit using micro:pegs to eliminate the problems associates with crocodile clips.

See the micro:pegs website for more information.

Algorithm:

     if button A is pressed

     turn pin 0 on

     display a message to say 'pin 0 on'

     pause for 1000 milliseconds

     turn pin 0 off

MakeCode Editor script:

It is not essential to display the message "pin  0 on", but for the beginner, it is reassuring, when things do not work as expected, to know that the file has been successfully downloaded to the micro:bit and is running. 






Micro Python script:

https://python.microbit.org/v/3/



from microbit import *



while True:


   if button_a.is_pressed():

        pin0.write_digital(1)

        display.scroll('pin0 on')    

        sleep(1000)

        pin0.write_digital(0)

        display.scroll('pin0 off')

See screen shot of the micro:bit Micro Python editor below.


Input

An input is used to add some form of data to a program as it runs. For control purposes the input may be a switch being switched on or data entered via some form of sensor. The data may be digital (on/off) or analogue (a range of voltages). 


Task:

Respond to a digital input to pin 2.


Algorithm:

display the message 'press pin 2'

     repeat the following forever

          if pin 2 is pressed then display the message 'on' 

 else display the message 'of'      

The scripts below will display the message 'press pin 2' and then continue to wait for a digital input to pin 2. That is, a connection has been made between 3V and pin 2. 

If a connection has been made the message 'input 2 on' will be displayed.

There are lots of different ways to make a connection between 3V and pin 2. A simple push to make switch is the easiest as shown in the picture.


MakeCode Editor script:

Micro Python script:

from microbit import *


display.scroll('press pin 2')  


while True:

    if pin2.read_digital()==1:

        display.scroll('on')    

    else:

        display.scroll('off')


See screen shot of the micro:bit Micro Python editor below.