1- Input/Output
2- ------------
1+ Input/Output Pins
2+ -----------------
33
44There are strips of metal along the bottom edge of the BBC micro:bit that make
55it look as if the device has teeth. These are the input/output pins (or I/O pins
66for short).
77
88.. image :: blue-microbit.png
9+ :width: 300px
10+ :align: center
11+ :alt: micro:bit with pins labelled
912
1013Some of the pins are bigger than others so it's possible to attach crocodile
1114clips to them. These are the ones labelled 0, 1, 2, 3V and GND (computers
1215always start counting from zero). If you attach an edge connector board to the
1316device it's possible to plug in wires connected to the other (smaller) pins.
1417
15- Each pin on the BBC micro:bit is represented by an *object * called ``pinN ``
16- where ``N `` is the pin number. So, for example, to do things with the pin
17- labelled with a 0 (zero), use the object called ``pin0 ``.
18+ On the latest micro:bit **V2 ** the micro:bit logo can also be used as a touch
19+ input.
1820
19- Simple!
21+ In MicroPython, each pin on the BBC micro:bit is represented by an *object *
22+ called ``pinN ``, where ``N `` is the number pf the pin.
23+
24+ For example, to use the pin labelled 0 (zero), you can use the object called
25+ ``pin0 `` in your script. The logo pin **V2 ** uses ``pin_logo ``.
2026
2127These objects have various *methods * associated with them depending upon what
22- the specific pin is capable of.
28+ the specific pin is capable of eg. read, write or touch .
2329
2430Ticklish Python
2531+++++++++++++++
2632
2733The simplest example of input via the pins is a check to see if they are
28- touched. So, you can tickle your device to make it laugh like this::
34+ touched. So, you can tickle your micro:bit to make it laugh like this::
2935
3036 from microbit import *
3137
@@ -35,18 +41,32 @@ touched. So, you can tickle your device to make it laugh like this::
3541 else:
3642 display.show(Image.SAD)
3743
38- With one hand, hold your device by the GND pin. Then, with your other hand,
44+ With one hand, hold your micro:bit by the GND pin. Then, with your other hand,
3945touch (or tickle) the 0 (zero) pin. You should see the display change from
4046grumpy to happy!
4147
48+ When you use the latest micro:bit **V2 ** you can also change the default
49+ behaviour of the pin, so that you don't have to touch GND at all.::
50+
51+ from microbit import *
52+ pin0.set_touch_mode(pin0.CAPACITIVE)
53+ while True:
54+ if pin0.is_touched():
55+ display.show(Image.HAPPY)
56+ else:
57+ display.show(Image.SAD)
58+
59+ The default for the edge connector pins is `resistive ` and the logo pin
60+ **V2 ** is `capacitive `.
61+
4262This is a form of very basic input measurement. However, the fun really starts
4363when you plug in circuits and other devices via the pins.
4464
4565Bleeps and Bloops
4666+++++++++++++++++
4767
48- The simplest thing we can attach to the device is a Piezo buzzer. There are two
49- types of piezo buzzers. The simplest type to use are called active buzzers.
68+ The simplest thing we can attach to the micro:bit is a Piezo buzzer. There are
69+ two types of piezo buzzers. The simplest type to use are called active buzzers.
5070Active buzzers contain an oscillator that produces a tone at a predetermined
5171pitch when a current is passed through them. Passive buzzers require an
5272oscillating current to be passed through them to produce a tone at the frequency
@@ -55,11 +75,17 @@ one tone, while passive buzzers are slightly more complicated to use but can
5575produce a variety of tones.
5676
5777.. image :: piezo_buzzer.jpg
78+ :width: 250px
79+ :align: center
80+ :alt: piezo buzzer
5881
5982We're going to use an active piezo buzzer for output. To attach one to your BBC
6083micro:bit you should attach crocodile clips to pin 0 and GND (as shown below).
6184
6285.. image :: pin0-gnd.png
86+ :width: 250px
87+ :align: center
88+ :alt: piezo connected to pin0 and GND
6389
6490The wire from pin 0 should be attached to the positive connector on the buzzer
6591and the wire from GND to the negative connector.
0 commit comments