# The Digital Die

## 1 The problem with Microcontrollers in the real world.

### Learn It

• You should know that digital data is represented by 1s and 0s.
• We often regard 0 as being off (0V) and 1 as being on (5V - or whatever the supply voltage is).
• So in a simple circuit, we could use a switch to control the digital data. When the switch is closed, current flows and therefore it is on. If the switch is open, no current flows and therefore it is off.
• We can use this knowledge to build a really simple circuit.

### Build It

• We'll use circuit wizard to try and set up a simple digital circuit - Remember to always select the 'Circuit with Genie Flowchart' option when starting a new file.
• We want to use a microcontroller (in this case a GENIE C08), to turn an LED on and off.
• We also want to use a 9V battery to power our circuit.
• Set up a circuit like the one shown below.

### Test It

• We'll need a little program to go with our circuit, before we can test it out.
• Create a flowchart like the one shown below.
• Now flip back to the circuit diagram and test your circuit to make sure that it works.

### Try It

• So in theory we now have a circuit where we can control an LED using a microcontroller.
• But the real world is not so simple.
• Go to `Project>Simulation` and turn on Explosions.
• Simulate your circuit again and see what happens.

### Learn It

• It turns out that in the real world, GENIE chips don't like such high voltages.
• To make sure we don't destroy our chips, we need to make sure we keep the supplied voltage below 6V. 5V is always a safe bet.

### Build It

• Getting 5V from batteries isn't easy. We could use 3 x 1.5V AA batteries to give us 4.5V, and that would probably work, but that would cost us more money.
• Instead we'll use a component called a `Voltage Regulator` to reduce the voltage we supply to the GENIE chip.
• You can find the Voltage Regulator component in the Power Supplies menu on Circuit Wizard. It defaults to the 7805, which is the one we want to use.
• You will find the Voltage regulator (7805) under 'Power supplies' in the drop down menu.
• At first it looks okay, but notice the that LED is illuminated. This means that the output pin on the GENIE chip is high, even when the switch hasn't been closed.
• Now look at the video below, and you'll see that everything is not quite so awesome…
• Whether the switch is open or closed, the LED stays illuminated.

## 2 Pull Down Resistors

### Learn It

• Whenever an electrical engineer has a bug in her circuit, she'll grab her multimeter to see what's going on.
• You can find a `digital multimeter` in the Virtual Instruments menu.
• We'll place one of the probes on the digital input and the other probe on the ground rail.
• Please note: The input pin has moved from G1/A to G3/R, this is because G1/A is used for both analogue and digital inputs, but G3/R is digital only. The switch is digital, so the best input to use is G3/R.
• You will find the Voltage regulator (7805) under 'Power supplies' in the drop down menu.
• You will find the Digital Multimeter (XMM1) under 'Test Instrument' in the drop down menu (right at the bottom).
• Simulate your circuit again and note down the voltage readings when the switch is open and closed.

### Learn It

• Hopefully you can see that even when the switch is open, there is a Potential Difference between the input pin and the 0V rail.
• Microcontrollers in reality treat voltage ranges as being equivalent to a 0 or a 1.
• For a pin to be considered high, the voltage between the pin and ground should be 0.8 x the supply voltage.
• For this circuit that means the GENIE chip will read a pin as high (1) when the potential difference is above 0.8 x 5V = 4V
• For a pin to be considered low, the voltage between the pin and ground should be 0.2 x the supply voltage.
• For this circuit that means the GENIE chip will read a pin as low (0) when the potential difference is below 0.2 x 5V = 1V
• As you can see from your readings, the voltage isn't clearly above 4V or below 1V. This means the microcontroller doesn't know if the pin is high or low. We describe this as being a floating pin, and it can cause all kinds of unpredictable behaviour. A pull-down resistor would tie the pin to ground and ensure that when the switch is not pressed, the pin remains below 20 percent of the supply voltage and as such, is recognised as a low/zero input.

### Try It

• Luckily there is a solution. Place a 10kΩ resistor between the pin and the ground rail, as shown below.
• You will find the Voltage regulator (7805) under 'Power supplies' in the drop down menu.
• Now you'll see that the input voltage when the switch is open, the voltage should be 0V and when the switch is pressed it should be 5V (it might be slightly higher - 5.41V)/
• This type of resistor is called a pull down resistor and ensures that the GENIE chip reads a logical 0 when the switch is open and a logical 1 when the switch is closed.

### Learn It

• Let's see if the 10kΩ resistor helps us on our real circuit.
• Once again everything is awesome…

• What voltages are required to give a logical 0 or 1 on an input pin for a GENIE chip?
• What is meant by a floating pin?
• How does a pull down resistor prevent a pin from floating?
• This is the Gold badge - Progress ladder, Green - Research a project in a variety of ways without assistance.

• You can use a virtual breadboard to build the circuit shown in the videos above, and test out what happens when you add a pull down resistor yourself.
• Here is a diagram that can help you to build the board without a pulldown resistor.
• You will need to click on the 'PCB Layout' tab at the bottom of the Circuit wizard design to be able to find and select breadboards.
• You need to choose the '390-pin' Breadboard
• The 'C08' chip is the Genie 08 IC, you will find it in the 'Microcontrollers' option in the drop box.
• For the PTM switch, please use the 10X10mm push switch in the 'switches' option in the drop box.
• For the 9V battery, this will be found at the bottom of the options under 'Off board components' the 'power supplies' - the 9V batteries are right at the bottom of the options.
• Use the 7805 voltage regulator.
• Use the 5mm LED.
• Build the virtual breadboard above and design a flowchart program that will make the LED flash when the switch is pressed.
• Think where the pulldown resistor would go and then add it to the circuit.
• Test it out again.
• Once you have designed the circuit, take a screenshot of the breadboard without the pull-down resistor, with an explanation of how the circuit worked.
• Now take another screenshot with the pull-down resistor in the circuit, with an explanation of how it worked.
• This is the Platinum badge - Progress Ladder, Green - Use a breadboard to model a simple circuit (e.g. LED torch), with assistance.