Day 1
Summary of This Lesson
This lesson will be an introduction to more complex sensors. The basic sensors that have been covered are things like limit switches or magnetic reed switches, which can be used to detect motion or the position of an object through physical contact or the presence of magnetic fields. More complex sensors can be used to help detect things like the distance of an object, if an object has passed a certain threshold, or to measure the brightness of an environment. To help us understand how a sensor can deliver information, we will use different tools like oscilloscopes and multimeters. We have used multimeters to measure voltage, current, and resistance before, but the oscilloscope is very useful to measure the voltage over time which is plotted on its inbuilt screen. We will go over the basics of an oscilloscope and how to use it in this lesson as well.
Lessons for Electrical Training on the website requires a Tinkercad account at www.Tinkercad.com, so if you haven't already, make an account on that website. Also, you can press the "Try Circuits" button in the circuits tab of Tinkercad to get an introduction of how to use Tinkercad's circuit simulation feature.
Ensuring Accurate Control
Powering something like a motor is cool, in that you can make something move, but it can be a little tricky to try and move something very accurately and precisely. Sure, you can power a motor with a specific amount of voltage and current, but it won't be as accurate as say, 150 RPM. Different currents, caused by the different voltages, in a motor make it spin faster or slower, but how can we know exactly how fast it is spinning? Sensors can help with that. In fact, many sensors are able to help us measure other things, like distances between itself and whatever is in front of it, color, brightness, magnetic fields, you name it.
So, What are Sensors?
Sensors are electronic components that send and/or receive electronic signals, which are really just voltages, to a controller/microcontroller like an Arduino. These voltages that are sent can be used to find out what is going on. When a sensor detects something that it was meant to detect, it sends voltages in a specific fashion, whether it be basic like a constant voltage, or something more complex, like a PWM signal, which you've played around with on the Arduino before.
Let's Look at some Examples
Before we see how sensors really work and actually work with them, let's just take a look at some examples of sensors. Below is a Tinkercad Demo with some examples of sensors. In this demo, you will see the use of a photoresistor, passive infrared sensor, and an ultrasonic sensor. The photoresistor changes its resistance based on light, which decreases as the amount of light increases. This increase in light is demonstrated by the LED getting brighter. The passive infrared sensor, or PIR, detects motion, so if motion is detected, then it sends a voltage signal to the Arduino, making the Arduino light the LED up. The Ultrasonic sensor measures distance by receiving a voltage signal from the Arduino which tells it to send an ultrasonic wave, then, when that wave is received, the ultrasonic sensor sends a voltage signal back to the Arduino, which the signal lasts for time it took between when the ultrasonic wave was sent, to when it hit the receiver of the ultrasonic sensor. This gets the time it took for the wave to have a roundtrip to and from whatever object it hit. Then we can use math and the speed of sound to get the distance through programming. You'll see what these sensors do once you play around with them.
These sensors can be very useful to know what is happening around say, a robot or an Arduino. There are many more sensors than these which have their own purposes, pros, and cons. Generally, sensors with a board, called a printed circuit board (PCB), that have electronic components on them have a similar pattern to them. They generally have a pin for power, denoted by "VCC", a pin for ground, denoted by "GND, and other pins that pertain to the purpose of the sensor, like ECHO or TRIG for an ultrasonic sensor.
What is an Oscilloscope?
This may seem unrelated to sensors, but we will get to how they can relate later. So, you've used multimeters before, with their voltage, current, and resistance settings. Real multimeters also have more specific options for voltage, current, and resistance, but it also has things for diodes, transistors, checking electrical connections, etc. An oscilloscope can only measure voltage. So how is it useful, if not, more useful than the multimeter? The advantage that the oscilloscope has is that it can plot voltage over time on a graph. That graph is shown on a screen that a digital, or analog, oscilloscope has. It has probes, similar to a multimeter, which measure voltage. That voltage is taken by the oscilloscope and plots it on the graph over time. The oscilloscope has various options like changing the scale of the graph in the vertical or horizontal directions to better see the function produced by the voltage, making the function look clearer, subtracting the y values of 2 functions to make a third one, and much more. It may seem daunting to look at, but those knobs and buttons are there to help you, and also there are labels that put their function in context.
Importance of Oscilloscopes
Oscilloscopes are pretty neat tools, but it can also help you learn about electronics, especially when we see changing voltages used to deliver information, or just changing voltages due to what's happening in the circuit. This can help us determine how a circuit behaves, while teaching you how electronics work. I never had an oscilloscope myself, and I would've loved to have one to help me learn about electronics, which I am still learning. Fortunately, the school has a couple that the robotics team can use. The thing is, oscilloscopes are unfortunately expensive, at least the ones you and I would normally see, like the one in the picture above.
Using an Oscilloscope
The in-person people will get to learn and use a real oscilloscope, but don't worry, there is an oscilloscope in Tinkercad that you can use. However, the Tinkercad oscilloscopes do not have any knobs or buttons, making it less clunky to use, but you will miss out on the actual experience of using a real oscilloscope. At least you will have the experience of seeing voltages plotted over time, as well as being able to change how much of what's plotted, which is by changing the horizontal scale, or how wide you want your "point of view" to be.
On a real life oscilloscope, turn it on by pressing the on/off button. Pretty simple. If there isn't a probe like the one in the picture below, then connect it to one of the channels, which can be labeled X and Y. Don't connect it to the "Ext" channel. If you press the "start stop" button, then you can start probing circuits to have the voltage plotted for you on the screen. You might see very wobbly or fuzzy lines, signals moving from left to right aggressively, it being too small, etc. We'll go over how to make the signal look better later.
In real life and in Tinkercad, there are two probes which you use to measure the difference in voltage between two points, one probe being for ground which is attached to the main cord, and the other for a higher voltage than what the ground probe is at. So, if you probe one point which has 5 volts, and probe another point which has 2 volts, then the voltage plotted would be 3 volts. Usually, one probe would be connected to ground, and the other probe connected to the thing you want to see the voltage of. This is to ensure you get the voltage relative to the power source that the circuit you are probing uses.
In real life, an oscilloscope's probes has two options which are selected using a slider. On option being 1x, and the other being 10x, but what do these mean? Those options change the attenuation. Essentially, changing the attenuation of a probe allows it to work with different frequencies, which in electronics would mean a faster or slower alternating current, so if electrons are moving back and forth very quickly, then that is a higher frequency. By the way, frequency is measured in hertz, which is how many cycles are done per second. So if one cycle of a wave, meaning it rises, falls, then comes back to the original "height", finishes in one second, it has a frequency of one hertz. If two cycles happened in one second, then it has two hertz. What does it actually do when you change the slider option? Well, it just adds a resistor, and a component called a capacitor to the probe's circuit. That's right, the probes have circuit components in them. There are generally just capacitors and pretty high resistance resistors inside. The resistors are to prevent too much current from being taken away from the circuit/sensor you are testing as well as to measure the voltage, which is what you want. The capacitors help to smooth or dampen out signals. You can watch the video below to see how capacitors work, but essentially, they are like temporary batteries that can provide temporary electrical charge to fill in the times when the main source of electrical charge isn't present. So when would you use 10x versus 1x attenuation? For starters, you can generally use the 10x attenuation option as it has a good tradeoff, but you will need to adjust a variable capacitor by turning a screw with a small screwdriver. 1x is for lower frequencies.
If you are using the 10x option, then you will have to probe a test signal from the built-in function generator of the oscilloscope. The picture to the right highlights in blue where the built-in function generator metal connectors are. The connectors are where you would connect the probes. You should see a signal that the built-in function generator created. The function you should see is a square wave function, which is a wave that has sharp, 90 degree turns, and looks blocky. That's what it should look like, but if your probe isn't tuned, which will happen, then the turns don't look quite 90 degrees. To tune the probe, use a small flathead screwdriver and find the screw on the probe. Turn the screw, which will change the square wave that you see. Turn it so that you get 90 degree turns on the square wave. Once you do that, it is tuned!
To get a clearer picture
You might see the square wave, or any graph, as a very "zoomed out" picture. You can use the knob in the horizontal section to zoom in when you turn the knob.
Another thing you might encounter is a graph that goes back and forth horizontally. This is because the oscilloscope doesn't know when to start measuring the function. If you change the "level" knob to be in the middle between the top of the function and the bottom, then it will know when to start measuring. This is caused because the oscilloscope starts measuring when the voltage starts rising, or starts lowering, depending on what setting is picked. If the "level" line is at a point where the voltage is not changing, neither rising or lowering, then the oscilloscope gets confused, making the function move left to right quickly.
This is the basics of oscilloscopes. The best way to learn is by using it however. The second best way could be through a video. So here's a video that covers what was above and more.
Let's use Sensors and Probe them!
We will be using different sensors and probing them with the oscilloscopes to see how they work.
Potentiometer
We've used these multiple times before, so let's use it again as a test to see how an oscilloscope can plot voltages. Go into Tinkercad and create a new project. You will try to make a circuit with an Arduino, LED with resistor, and a potentiometer. Use DIO pin 6 to power the LED, and analog pin A0 for the potentiometer to take in the analog voltages. The other two pins of the potentiometer connect to either GND or 5V. Once you get that setup, copy and paste the code below into that Arduino by clicking on it, pressing the "code button", pressing the drop down menu that says "blocks" then choosing "text", then replace the text to the right with the code you've copied. This will control the Arduino to make the LED brighter or dimmer depending on the potentiometer's handle position. Here's the code:
float LED_Brightness = 0.00;
void setup()
{
pinMode(6, OUTPUT);
pinMode(A0, INPUT);
}
void loop()
{
LED_Brightness = (analogRead(A0)/1023.00*255);
analogWrite(6, int(LED_Brightness));
delay(100);
}
Now, search for an oscilloscope in the search bar, and connect the negative side to the GND pin of the Arduino, and the positive side to the middle pin, which is the pin that connects to A0. Now if you turn the potentiometer, you can see how the voltage changes over time, which is also seen by the brightness of the LED. This should show you how the change in voltage means a change in LED brightness.
Ultrasonic sensor
This will be the first time you get to work with the Ultrasonic sensor, so here we go. For this one, we won't be using an LED, since the LED won't get brighter due to any change in voltage, but in terms of the distance that the ultrasonic sensor detects, which doesn't relate to voltage. The ultrasonic sensor has 4 pins, a GND, Vcc, TRIG, and ECHO pin. GND is where you connect the GND of the Arduino to, to provide a 0v reference. Vcc is where you power the sensor with 5V, so 5V from the Arduino goes into that pin. TRIG and ECHO go to the DIO pins. The TRIG pin is used to make the sensor send out an ultrasonic wave, which happens when that pin receives 5 volts, or a high signal. We will activate that pin using DIO pin 5. When the ultrasonic wave bounces back to the ultrasonic sensor, the ECHO pin sends a bunch of pulses of voltages that go to 5V and back to 0V. Each pulse lasts for the time it took for the ultrasonic wave to go a roundtrip from the ultrasonic sensor, to the target, and back again. We can use that time the pulse was HIGH for to get the distance with math. In order to get the length of time of that pulse, we will use DIO pin 4 to connect to ECHO. That should be it for the wiring. Once you get that setup, copy and paste the code below into that Arduino by clicking on it, pressing the "code button", pressing the drop down menu that says "blocks" then choosing "text", then replace the text to the right with the code you've copied. This will allow the Arduino to measure distance with that sensor. Here's the code:
const int ECHO = 4;
const int TRIG = 5;
const int LED = 13;
int TimeToTarget = 0;
int DistanceToTarget = 0;
void setup()
{
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(8);
digitalWrite(TRIG, LOW);
TimeToTarget = (pulseIn(ECHO, HIGH)); //Time to target and back in microseconds
DistanceToTarget = (TimeToTarget/2*0.0343); //Distance to target
Serial.println(DistanceToTarget);
if (DistanceToTarget > 100){
digitalWrite(LED, LOW);
}
if (DistanceToTarget <= 100){
digitalWrite(LED, HIGH);
}
delay(100);
}
This code also has an accommodation for an LED pin at DIO pin 13, so if you want, you can wire an LED with a resistor there too to see it light up when the ultrasonic sensor is at a specific distance from an object. Now, connect two oscilloscopes to the ultrasonic sensor. Both of the oscilloscope's negative side connect to the GND pin of the ultrasonic sensor to compare the voltages of the ECHO and TRIG pin. One oscilloscope's positive side connects to ECHO, while the other connects to TRIG. Run the simulation, and you should see different graphs on the oscilloscopes. The oscilloscope connected to TRIG only has a small hump, which in this case is a square wave, because there is a very sharp change from 0V to 5V. When the Arduino wants the ultrasonic sensor to send out an ultrasonic wave, the Arduino turns the DIO pin connected to TRIG on to provide 5V quickly, then off to 0V. The other oscilloscope shows multiple square waves when the ultrasonic waves bounce back into the ultrasonic sensors receiver. We would focus in on one signal, so if you changed the time per division from 100 ms to 10 ms, it in effect zooms in. If you were to click on the ultrasonic sensor and move the circle around that pops up, you will see the length of the pulse change, which correlates to the distance to the target.
That's basically it
You've got an intro to more complex sensors, as well as learning how to use an oscilloscope which helped us to learn more about sensors in the process. There are many different sensors out there which you can search on youtube for. To start, you can search for ultrasonic sensors, IR break beam sensors, and PIR sensors. The in-person people will get to play around with different sensors, so you will be missing out on that, but either way, you will be working with real sensors on the robot, so don't feel too bad.
Day 2
Summary of This Lesson
In this lesson, members will learn how the IR break beam sensor works and how to use them. We will also try to use other components with it to simulate sensors working together with things like motors. Since there isn't an equivalent component to the IR break beam sensor, this will mainly be for documentation for how the in-person meetings go, as well as documentation for the sensor itself.
Lessons for Electrical Training on the website requires a Tinkercad account at www.Tinkercad.com, so if you haven't already, make an account on that website. Also, you can press the "Try Circuits" button in the circuits tab of Tinkercad to get an introduction of how to use Tinkercad's circuit simulation feature.
IR break beam sensor, what is it?
An IR break beam sensor is used to see if there is an object that is between two points. It can be used to see if balls pass by an opening, or counting things that go by, but generally, it is used to see if there is something in between the two sensors.
How does it work?
There are two sensors in a set of IR break beam sensors. One is a transmitter, and the other being a receiver. The transmitter is powered by a component like an Arduino continuously, with two wires, and sends out an IR, or infrared, signal continuously. This IR signal is then received by a receiver, which is connected to a component like an Arduino. The receiver has one more wire than the transmitter. This wire connects to a transistor inside of the IR break beam sensor, so this transistor is like a switch, meaning it can create floating signals like a normal switch would due to the voltages in the environment. We would need a pull-up resistor for this, meaning the signal wire connects to 5V in parallel to the DIO pin of the Arduino or another controller. When there is an object in between the two, the state of the transistor changes, which changes the voltage going into the DIO pin connected to the signal wire, which can let the Arduino or another controller know that an object had went in front of it. When an object is in between the two sensors, the transistor allows electricity through to go to ground, which redirects the current at the 5V pin to go through the pull-up resistor, and to ground, and not to the DIO pin, making it read a digital 0, or 0V. The opposite happens when there is no object in between. The transistor is open, which means that the current from the 5V pin has to go through the resistor and to the DIO pin, since that is the only path, making the DIO pin read a digital 1, or 5V. So, when there is an object in between the sensors, the DIO pin reads 0V, and if there isn't and object, it reads 5V.
Using the Sensor
Unfortunately, there is no Tinkercad equivalent to the IR break beam sensor, so you will have to bear with us. Once you wire up the sensor described above, you are set to go. All that's left is to get the program up and running and you can start detecting things in between the two sensors.
Using an Oscilloscope to see how it works
In-person members will get to use a real IR break beam sensor and will also get to probe the two sensors with an oscilloscope to see the voltages changing when an object passes between the two sensors. They will also get to see how the software works with the sensors.
Worth checking Documentation for any Component
Every electronic component has documentation which you can look at to see the characteristics and specifications of that component. It can also help you use that component. Here is a simple documentation document for an IR break beam sensor. FHYL99WJHUTRA1P.pdf (instructables.com)
Here is another example for an ultrasonic sensor: HC-SR04 User Manual.pdf (maine.edu)
End of the meet
At the end of the in-person meet, members will get to play around with the IR break beam sensor and will be able to use it with different components.