Programmable Circuits

Cymraeg

THIS PAGE IS UNDERGOING CHANGES - PLEASE RETURN SHORTLY

Beginners Guide to Programmable Circuits using Tinkercad

An online activity on how to design, create, and program electonic cicruits using Tinkercad.

We have designed this to follow on directly from our Introduction to Circuits using Tinkercad activity.

As we shall be using BBC Micro:Bits as the programmable controller in this session, we would recommend those of you unfamiliar with this computer to visit our Online Micro:Bit Workshop Series first.

Click on the below headings to expand or collapse each section.

Before we begin, test yourself with the following circuit diagram symbols, how many can you identify?

A symbol which looks like two H's where the same side of each 'H' is longer in both directions
A circle where the wires enter slightly on both sides and are then joined by a half-circle arc
A straight line
 A small circle with a straight line from the centre that connects to the left side of another equal sized circle directly below.
A circle with a cross inside
A circle containing a triangle with two parallel arrows pointing from one triangle edge to outside the circle overlaying the wire line.
A small circle above another with a straight line from the bottom centre that goes out in a slight diagonal to show it is not connected to the bottom circle.
A circle with a large capital A inside
A rectangle with wire passing in and out of the shorter sides.
A circle with a large capital V inside



Recreate this circuit in Tinkercad:

A circuit connecting a 3V battery to a 1000kΩ resistor and a LED.

A circuit connecting a 3V battery to a 1000kΩ resistor and a LED.

Using 2 AA batteries instead of the button battery is also correct.


We shall be looking at a range of different electronic components, some are inputs and others are outputs.

It is important to know the difference, especially when writing a program.

Input: A device/component which sends signals (information) to the computer.

Output: A device/component which receives signals (information) from the computer.

Try and identify whether these computer components are inputs or outputs. Click on each component to reveal the answer.

INPUT

INPUT

OUTPUT

INPUT

INPUT

OUTPUT

It is also possible for a device/component to be both an input and an output. For example, a touchscreen is both as it has touch input and image output.

Now let us look at the components on a BBC Micro:Bit.

Inputs

Buttons A and B
Light sensor
Radio antenna
Accelerometer/Movement sensor
Compass
Touch sensor (v2 only)
Microphone/Sound sensor (v2 only)

Outputs

LEDs
Radio antenna
Speaker (v2 only)

Notice how the radio antenna is both an input and an output device. This is because it can both receive and send radio signals.


A BBC Micro:Bit can simply be used as a 3V power supply, with no programming required.

The GND pin acts as the negative terminal, whilst the 3V pin acts as a positive terminal.

Recreate this circuit in Tinkercad to see this:

A circuit with an LED and a 1 kΩ resistor connected to the 3V pin and GND pin on the Micro:Bit

When you run the simulation the LED should light-up.

You should also see a smiley face appear on the Micro:Bit screen - this is due to the default code used by Tinkercad to show the Micro:Bit has power.


A Tinkercad circuit with an LED and a 1 kΩ resistor connected to the 3V pin and GND pin on the Micro:Bit

Now, update your circuit to match the one below:

The same circuit with the addition of another LED and reistor connected to Pin 0 and GND

Have you noticed the arch in the wire? This is to illustrate that the wires are not connected - they are just crossing over each other.


A Tinkercad circuit with two sets containing an LED and 1kΩ resistor each. One set is connected to the GND and 3V pins of the Micro:Bit. The other to the GND and 0 pins.

When you run the simulator you will see that the LED connected to pin 0 does not light up.

This is because pins 0, 1, and 2 are programmable pins that require instruction from the Micro:Bit's program.


To access the programming editor on your circuit click on the code button (next to the start simulation button)

You can only access the code if you have a controller (Micro:Bit or Arduino) in the circuit build area.

We shall be using the Blocks option in the top left of this editor.

If you switch between blocks and text you will lose your program.

Remember that anything in the 'on start' loop only runs once when the program starts. Anything in the 'forever' loop will keep repeating until the program is exited.

You can click and drag the blocks from the side menu into your program.

You cannot edit your code if the simulation is running.


If a component only has two settings - on and off for example, this is a digital device. In Tinkercad we use a digital signal of HIGH to turn a component on. Setting the signal to LOW will turn the device off.

Here is an image of a basic Tinkercad program that will turn on a digital component connected to pin 0 on the Micro:Bit.

A forever loop containing a 'digital write pin P0 to HIGH' block

To turn off a digital component connected to the same pin we would change the HIGH to LOW:

A forever loop containing a 'digital write pin P0 to LOW' block

If you haven't already, create this circuit:

A circuit with two sets containing an LED and 1kΩ resistor each. One set is connected to the GND and 3V pins of the Micro:Bit. The other to the GND and 0 pins.

A Tinkercad circuit with two sets containing an LED and 1kΩ resistor each. One set is connected to the GND and 3V pins of the Micro:Bit. The other to the GND and 0 pins.

Now open the block code window and program your Micro:Bit to make the LED connected to pin 0 flash on and off.

Tip: The LED is an output.

Tip: You will need to use pauses between turning on and off. If these are not included, the simulation will either make the LED appear to be permanently on or off as it's running through the code too fast.


The program reads: Forever {'digital write pin P0 to HIGH', 'wait 1 secs', 'digital write pin P0 to LOW', 'wait 1 secs'}

Note how we need a wait block after each signal change, not just one in the middle.


Digital commands work well for turning on and off devices or for reading key/button presses.

However a lot of components have a range of values they can send and/or receive. These are called analog components/devices.

For example, the BBC Micro:Bits built-in light sensor measures light level on a scale of 0 to 255. The higher the value, the brighter the light detected. The microphone uses the same range of values for sound, the higher the value, the louder the noise detected.

This means that we need to use programming instructions that have more options than just HIGH and LOW.

Let's say we have an analog output connected to pin 0 of our Micro:Bit and we want to change its value, we'd need to use this block:

The forever loop contains an 'write analog pin P0 to 167' block

You will need to know the range of values for your component to ensure you only send a number that falls in it.


LEDs have the ability to be either a digital or an analog component.

How? Well, the LED has more options than just on and off, it can also shine at different brightness levels.

The full range of an LED is between 0 and 255. The higher the number, the brighter the LED will shine.

Returning to the circuit we created in the last exercise, replace the digital write blocks with analog write set to 255 and 0.

Tip: To delete blocks, click and drag them back into the block menu.


The program reads: Forever {'write analog pin P0 to 255', 'wait 1 secs', 'write analog pin P0 to 0', 'wait 1 secs'}

In the simulation, the program looks no different, because we're sending the same commands of fully on and fully off.

So, let us now have the LED flash on, somewhere in between (165 is a good mid-value), and then off.

Tip: Remember to add another wait block for whilst the LED is at this level too.


The program reads: Forever {'write analog pin P0 to 255', 'wait 1 secs', 'write analog pin P0 to 165', 'wait 1 secs', 'write analog pin P0 to 0', 'wait 1 secs'}

An RGB LED has three individually programmed colours (red, green, and blue). This allows the user to change the colour and/or choose a specific one.

To do this, the RGB LED has four pins, as shown below.

The RGB LED

An image of the RGB LED with the pins labelled from left to right as Red, Cathode, Blue, and Green.

Circuit diagram symbol for an RGB LED



Shown as a wire split into three parallel lines each with a equilateral triangle positioned with a tip pointing back toware the single wire. There is then a line, the same height as the triangle drawn at this tip. The triangles are coloured; the top is red, the middle is green and the bottom is blue. The wire ends are marked with a small circle to illustrate connection points.

Now, create this circuit in Tinkercad:

A circuit showing an RGB LED with the cathode connected to the GND pin on a Micro:Bit. The red pin of the LED is connected to pin 2 on the Micro:Bit, green to pin 1 and blue to pin 0.

A Tinkercad circuit showing an RGB LED with the cathode connected to the GND pin on a Micro:Bit. The red pin of the LED is connected to pin 2 on the Micro:Bit, green to pin 1 and blue to pin 0. The wires are coloured to match.

Note how I used different coloured wires to be able to easily see which colour is connected to each pin.

If you've re-used the Tinkercad file, then you'll need to delete the current code inside the forever loop.

We now need to write a program to activate this component in different ways.

  1. Have the RGB LED shine bright white when the program starts.

    Tip: You can use either the digital or analog options for this challenge. The provided answers use analog controls.

    White light is produced by using the maximum value (255) for red, green, and blue.


    The on start loop with three 'write analog to 255' blocks, one for each pin (0, 1, and 2).

  2. Have the RGB LED shine red when button A is pressed.

    Tip: Button A is an input.

    Tip: Set all three pins for this as we'll be using different colours on other inputs.

    One pin should be set to 255, while the others are 0.


    An 'on button A pressed' loop with two 'write analog to 0' blocks for pins 0 and 1 and a 'write analog to 255' block for pin 2.

  3. Have the RGB LED shine blue when button B is pressed.


    An 'on button B pressed' loop with a 'write analog to 255' block for pin 0 and two 'write analog to 0' blocks for pins 1 and 2.

  4. Have the RGB LED shine yellow when buttons A and B are both pressed.

    Yellow is a secondary colour of light, this means its a mix of two colours.

    For more information on how rgb colour mixing works, you can check out our Colour Workshop.


    An 'on button A+B pressed' loop with a 'write analog to 0' block for pin 0 and two 'write analog to 255' blocks for pins 1 and 2.

The BBC Micro:Bit includes a built-in light sensor. In Tinkercad, different light levels can be set using a slide scale on the Micro:Bit's sensor panel, which activates when you start the simulation:

A screenshot of the Micro:Bit sensor panel within the Tinkercad simulator with the light level slider highlighted.

We need to program our circuit to use the light level reading the sensor collects.

First, we need to know the range of the sensor - in this case, 0 - 255. We also need to know what the values mean, for this sensor, the higher the reading, the brighter the light.

To have a circuit that reacts to a sensor reading, we will need to use an if-statement in the program.

Below is an example if statement made using a combination of blocks from the control, maths, and input menus. This program will have all the LEDs that make up the Micro:Bit's screen turn on when the light level detected is less than 75, otherwise the screen is cleared (all LEDs turned off). In effect, it makes the Micro:Bit into a torch that activates itself when it is dark.

The Tinkercad block program inside the forever loop read as if light level < 75 then show leds (all), else clear screen.

Tinkercad only supports the first version of the Micro:Bit. This restricts how many built-in sensors we have available to experiment with.