things we will cover · - if you remember anything about kirchoff’s laws and v=ir from lesson 1,...

20
Analog and digital inputs Lesson 2 - Planned order to do things in: - What we’re going to cover - The potential divider circuit - Testing that the potential divider formula works with a multimeter - How to wire the potential divider to make a light- or temperature-sensing circuit - (How to choose the right resistor when using an LDR or thermistor, featuring datasheets again) - How to plug this all into the Arduino - How to use the noble Serial Monitor to graph your results (featuring lots of “copy&paste this code”) - Explanation of analog vs digital

Upload: others

Post on 30-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Analog and digital inputsLesson 2

- Planned order to do things in:- What we’re going to cover- The potential divider circuit- Testing that the potential divider formula works with a multimeter- How to wire the potential divider to make a light- or

temperature-sensing circuit- (How to choose the right resistor when using an LDR or thermistor,

featuring datasheets again)- How to plug this all into the Arduino- How to use the noble Serial Monitor to graph your results (featuring

lots of “copy&paste this code”)- Explanation of analog vs digital

Page 2: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Things we will cover

● What types of inputs can I add to my project?● How do I measure light levels and temperature?● What is the difference between ‘analog’ and ‘digital’?● How can I make light- or dark-sensing circuits using

potential dividers?

- Okay, so, now that you’ve had a bit of experience with electronics theory and wiring things up, we’re going to learn how you can add inputs to your project.

- Adding an input = allowing your project to react to changes in the environment, e.g. temperature, light level, etc.

- There are actually multiple kinds of input - you have ‘analog’ and ‘digital’ ones - and we’ll cover these later.

- Hopefully you’ll also start to get an idea of how you might tie this in with the Arduino Uno to make decisions based on input data - e.g. if it’s dark, turn on the light.

Page 3: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

The potential divider circuit● Looks like that thing over there ->● Questions:

○ What’s the circuit useful for?○ Where are the 5 volts going?○ What’s that strange Vout thing doing?

- Okay, so, when you’re dealing with inputs, there’s essentially one circuit you’re going to be using: the potential divider circuit.

- Again, this abuses the word potential - this is a circuit that divides ‘potential’ (i.e. voltage), instead of being an undecided circuit that might divide something, if it feels like it.

- If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need to go somewhere before we get to ground.

- Given that I have two resistors in the circuit, where’s the voltage going to go?

- Answer: by Kirchhoff’s Voltage Law, it has to be dropped across the two resistors. (this is on the next slide as well)

- Is it going to be distributed equally? What changes its distribution?- Answer: it depends on the resistance, because V=IR!

- What’s this Vout nonsense? (Answer, it’s a dangling wire where we’re going to take a multimeter reading from. You’ll see how it works.)

-

Page 4: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

The potential divider circuit

- There’s some maths behind how this works, but it’s literally only three lines of working. I think it’s interesting to show you how it works instead of just giving you a formula, because it reinforces the idea that V=IR is actually something useful!

- Try to understand this, but don’t worry if you don’t; it’s not required.- Okay, so, using V=IR, what would the voltage of this bottom resistor (R1) be?

I’ve got a current and a resistance, so the voltage must be…?- And, what’s the current going to be? Well, using V=IR again, we can figure out

that it’s just the total voltage over the total resistance for the entire circuit, because current is pretty much the same everywhere unless your circuit has multiple branches or something funky going on.

- And, substituting in I here, we just get our final formula.

Page 5: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

The potential divider formula● We can figure out how the potential divider works

using V=IR.● However, you don’t need to understand the derivation

to be able to actually use it (thankfully).● Just use this formula:

- Regardless of whether or not you got all that maths or not, here’s what you actually need to know [explain formula].

- V_in here is your supply voltage at the top - for an Arduino, it’s 5 volts.

Page 6: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Testing this out in practice● Pick two resistors of known values (we’ve got tons

in the back of the lab…)● Wire them up like in the circuit diagram● Check Vout with a multimeter!

- If you don’t believe me (and, to be honest, you really shouldn’t!), you can actually test the potential divider formula for yourselves!

- Just pick two random resistors (although make them relatively close to one another…) and put ‘em in a circuit, and measure the voltage with the multimeter.

- Notice that I’ve left out the power connections to the Arduino here, to see whether you remember how to do it from last time.

- (i.e. you need to connect 5V to the red rail, and GND to the blue rail…)

Page 7: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Quick background: why do we care about this?● Dividing voltage across two known resistors doesn’t seem that useful.

○ You’re just making the electronics do the same maths that you have to do.○ It’s also a bit of a waste of power (!)

● However… what if one of the resistors was variable?○ The potential divider circuit lets us, effectively, measure resistance.○ So, if we have something that changes resistance when something interesting happens, this

could potentially be useful…○ (if you’ve done GCSE Physics you probably already know about things that do this)

Page 8: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

New fancy components

LDR(light-dependent

resistor)

Thermistor

Light level / temperature increase -> resistance decrease

- So yeah, we actually have some fancy components that do just that: vary their resistance depending on some external factor.

- There’s the light-dependent resistor (or LDR), which varies in resistance with the amount of light falling on it, and there’s the thermistor, which does the same, except for temperature.

- They have these circuit symbols, and they look a little bit like this (although really they could take multiple shapes or forms; it’s just that ours look like this)

- One important thing to remember: as the light level or temperature increases, the resistance decreases.

Page 9: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

...in a potential divider

- And, of course, we can use these components in our potential divider circuit, like this example.

- If we wire this up, we should now get a value for Vout that changes according to the intensity of the light falling onto the LDR.

- Why have we put the LDR on the top? You’ll see later…! (it’s because we want a HIGH signal when the light falls on the LDR)

- However, we’re not going to use the multimeter this time!

Page 10: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Testing the new fancy components● These components vary their resistance a lot.● Multimeters don’t cope that well with things changing.

○ The scale can keep jumping everywhere, and it’s hard to get an ideaof how the voltage is changing over time.

● However… we have an Arduino!○ The Arduino has ‘Analog In’ pins that act as a limited

voltmeter: you feed in a voltage between 0 and 5V,and you can read it as a number between 0 and 1024in your code.

○ (It also has digital inputs and outputs...we’ll get to that later!)

-

Page 11: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

BreadboardingWe can use the Analog In pins (A0-A5) to read

the voltage in the middle of the potential divider.

However, we still need to figure out how to view

this on a computer...

- There are two types of input pin: analogue and digital; we’re using analogue here (for hand-wavy reasons)

- Important to note the distinction between analogue and digital pins here, and say we’re going to get back to it a bit later…!

Page 12: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Introduction to the Arduino IDE**(“integrated development environment”)

- To do that, we’re going to need to teach you a bit about how you actually load a program onto the Arduino. We’re not yet going to make you do any programming, but it’s worth knowing how to transfer code onto the Arduino anyway.

- The program responsible for that is the Arduino IDE (integrated development environment), which should be installed on everybody’s computer already.

Page 13: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Introduction to the Arduino IDE● Plug in the Arduino● Tell the IDE where the Arduino is:

○ Tools -> Board -> Arduino Uno○ Tools -> Serial Port -> select the one that isn’t COM1; it should be fairly obvious

● Note for later: you’ll need to remember to reselect the serial port next time!● Open the ‘Blink’ example

○ File -> Examples -> 01.Basics -> Blink

● Press the Upload button to send the program to the Arduino○ The LED marked ‘L’ on the board should flicker frantically for a while, and then start blinking

regularly.○ (This just tests that you’ve set everything up correctly.)

Page 14: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Flashing the measurement code● We’ve written some code which takes voltage readings and spits them out to

the computer over serial.○ (You’ll learn this later!)

● Using the Arduino IDE, open the code and upload it. (We’ll show you where the code is.)

● Once it’s uploaded, go to Tools -> Serial Plotter to look at the data!○ Try covering and uncovering the LDR,

and see how the value changes.○ Try swapping the LDR with the resistor

(preferably not while the power is on)

- We need to figure out where to put the code.- This code should do what we need.

Page 15: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

for LDRs:Light level increase -> resistance decrease

- Okay, so, you’ve hopefully now had a chance to play around with the potential divider circuit, and see what happens to Vout when you increase and decrease the light level!

- So, if we have the potential divider set up like this, with the top resistor as the LDR, what does happen to Vout as the light level increases? (This is question 1 - what happens to Vout when R1 is much greater than R2?)

- Remember from earlier that the resistance of an LDR decreases as the light level increases.

- [The answer is that you get a ‘high’-er value of Vout - i.e. it’s more than half of the supply voltage, or > 512.]

- If the LDR’s resistance is small compared to the other resistor, you should get a bigger value for Vout - because the fraction [R1 / (R1 + R2)] essentially just becomes R1/R1, which is 1.

- And, if you swap it round, and put the LDR at the bottom with light shining on it, what happens? (This is question 2.)

- This time, the fraction [R1 / (R1 + R2)] essentially becomes just 0 / R2, i.e. 0, so you get a low Vout.

- Hopefully what comes out of this is that the other resistor you choose when you’re using something like an LDR is important.

- (How do you get it? A datasheet again!)

Page 16: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

- Using the knowledge from the previous slides, can anyone draw a ‘light sensing’ circuit - a circuit that has a high Vout value when light shines on it?

- Can anyone do a dark sensing circuit?

Page 17: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Analog(ue) and digital

- Earlier, I kinda skimmed over why we were specifically choosing an analogue input pin to measure our signal. The potential divider is something which produces an analogue signal - which means that, if you plot a graph of voltage against time, like you did with the Arduino, you get something like this [i.e. the graph on the screen].

- As you can see, this signal can pretty much be anything between 5 volts and 0 volts - i.e. you can get something funky like 3.76V or whatever.

- The Arduino unfortunately is not that accurate, and only gives us a number: it sets 5 volts as 1024 and 0 volts as 0, and you get something within that range.

- However, an analogue signal isn’t that useful for making decisions. For example, if we’re doing the project where you have to turn a light on when it gets dark, you want a binary, yes-or-no value: is it dark enough to turn the light on yet, or not?

Page 18: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Analog(ue) and digital

“high”

“low”

- Therefore, we do this thing where you draw a line at half of your total voltage - in this case, 2.5V - and anything above that is “high” (i.e. on, or a 1), while anything below that is “low” (i.e. off, or a 0). For this graph, you can see some bits are below 2.5V, and are therefore counted as ‘low’, while it goes up and is ‘high’ for a while here [accompany this section with vague hand movements towards relevant sections of the graph].

- Obviously this feeds back into our potential divider: we ideally want to play around with it a bit and find the value of R that’s going to make the signal ‘high’ when we want it to be.

Page 19: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Analog(ue) and digital

“high”

“low”

- And then we can use this to plot a new signal, a digital signal, which has to be either 0V or 5V (depending on whether we’d consider the signal ‘low’ or ‘high’), and so you get this nice blue line.

- That’s essentially the difference between analogue and digital - an analogue signal can be anything within a given range of values, whereas a digital signal has to be one of a few defined values, like 0V or 5V (and not anything in between).

- (the maths people would call ‘analogue’ ‘continuous’ and ‘digital’ ‘discrete’)

- Aaand this is what the ‘digital’ inputs on your Arduino do - they give you either a HIGH or a LOW value, and nothing in between.

- (They can also output digital signals, but we’ll get to that!)

Page 20: Things we will cover · - If you remember anything about Kirchoff’s Laws and V=IR from lesson 1, you surely know that, since we have 5 volts in this circuit, all 5 of them need

Things we covered today

● What types of inputs can I add to my project?● How do I measure light levels and temperature?● What is the difference between ‘analog’ and ‘digital’?● How can I make light- or dark-sensing circuits using

potential dividers?

(Next time: programming!)

- Okay, so, now that you’ve had a bit of experience with electronics theory and wiring things up, we’re going to learn how you can add inputs to your project.

- Adding an input = allowing your project to react to changes in the environment, e.g. temperature, light level, etc.

- There are actually multiple kinds of input - you have ‘analog’ and ‘digital’ ones - and we’ll cover these later.

- Hopefully you’ll also start to get an idea of how you might tie this in with the Arduino Uno to make decisions based on input data - e.g. if it’s dark, turn on the light.