
Ultrasonic Sensor Lesson for Arduino
Explore how ultrasonic sensors work, along with Arduino programming, to create a self-driving car. Learn about electric motors, serial monitoring, sensor functionality, and more in this engaging Year 7 lesson.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Year 7 Ultrasonic sensor and serial monitor Lesson 5
I acknowledge the Original Custodians of this land and pay respect to Elders past, present and emerging.
Lesson Outcomes Revise how electric motors work Learn how an Ultrasonic sensor works Describe how a serial monitor works Learn about the use of variables Learn about the use of libraries to expand the functionality of the Arduino Propose applications and adjustments for applying ultrasonic sensor to self-driving car
Revision: electric motors What does this code do? Have a think for a minute, share with a group, then share with the class.
Revision: electric motors It should make the car drive forward in a straight line forever.
Ultrasonic sensor An ultrasonic sensor is similar to a sonar used in boats An ultrasonic sensor shoots sound out of one of its two eyes . It waits for an echo to bounce back to the second eye By measuring how long it took for the echo to return, we can figure out how far away an object could be What would be some limitations of this method of measuring distances? What would be some limitations of this method of measuring distances? This Photo by Unknown Author is licensed under CC BY-SA
Ultrasonic sensor and Arduino In order to read the information from an ultrasonic sensor we must first learn about the serial monitor. The serial monitor can display information read off sensors within the Arduino IDE software. The serial monitor is opened by clicking on the magnifying glass
Serial Monitor Start the serial output Serial.begin(9600); Print something Serial.print( Hi there );
Serial.print Using Serial.print( red ); should make a continuous line of the word red
Serial.println Using Serial.println( red ); should make red continuously appear down the monitor
Serial Monitor What do you think this will do on your serial monitor?
Variables Variables are bits of memory we can use on the Arduino to store information When we declare a variable we have to give three bits of information; 1. What type type of information will be stored, is it a number, text or some other sort. This helps the Arduino know how much memory to put aside 2. A name name, what do we want to call this variable. This will be what we call when we read or write data to it 3. A value value, if we don t provide anything here it is blank Variables can be set at the start of the code and can be updated while the code is run Type value int Whole number (no decimal points) float Number with decimal points string words *There are many others We have created a bit of memory so whenever we type variable the Arduino will know we mean 1 .
Variables Use this code and see what happens on the serial monitor. Change the bit of memory (variable) from 1 to something else. Try again.
Creating a growing variable The bit of memory (variable) is declared at the start as 0. Check the serial monitor. Create your own growing variable. The bit of memory will get bigger by one every time
Libraries Libraries allow us to add new commands to the Arduino They are written by lots of different people and make it easier to add new hardware Can be added in 2 ways Manage Libraries (if internet connected) Add .ZIP (if no internet)
Manage libraries Searches the internet for libraries
Local libraries Allows us to add files from a USB or local drive
Library for Ultrasonics For ultrasonics we will use the NewPing library Either search or use the USB stick
Ultrasonic sensors and Arduino Now it is time to wire our ultrasonic sensors. 1. Connect VCC 2. Connect GND 3. Connect Trig 4. Connect Echo 5. Leave the OUT pin alone VCC to pin 6 6 on Arduino GND to pin 11 11 on Arduino Trig to pin A1 A1 on Arduino Echo to pin A0 A0 on Arduino It will be written very small on your own ultrasonic sensors.
Ultrasonic sensors and Arduino We include the NewPing library to make the extra commands available to use Declare some variables we are going to use Define our sonar object, this the name we use for the ultrasonic. We use some of the variables here Start the serial monitor We are using the Arduino to power the Ultrasonics. Making pin 6 HIGH means it outputs 5V. Making pin 11 LOW means it is GND (0V) Here is our code; 1) Wait 50 milliseconds every loop (we need to do this) 2) Run the command sonar.ping_cm sonar.ping_cm() () and put the output into the variable distance distance 3) Print the values out of distance to the serial monitor
Ultrasonic sensor and Arduino What does your serial monitor say? How accurate is your ultrasonic sensor? Do you think you should make the delay longer to make it easier to read?
STOP! It is all well and good to have a working ultrasonic sensor, but how does this help us with our self-driving cars? What purpose does an ultrasonic sensor have in a self-driving car? Do we know any code that will help us to use ultrasonic sensors with our motors to create a self-driving car?
Adding more sensors 1 ultrasonic is good but we will need more for our car. The car will have 3 ultrasonics so we need to program 3 sensors Each sensor will need its own; TRIGGER_PIN ECHO_PIN Variable to store its reading in A name We will use the same value for MAX_DISTANCE
QUICK!!! Get in your pairs/groups and write down the five most important pieces of information you have learnt today. Make sure the code that you have learnt today is recorded somewhere to use again.
Pack up time! Please pack up your kits ready for the next class Check that everything on this page is in your kit Male to female wires This Photo by Unknown Author is licensed under CC BY-SA Ultrasonic sensors
Acknowledgements MIE School is a widening participation program developed by USC to increase participation in tertiary education. It is partly funded by the Australian Government Department of Education and Training under the Higher Education Participation and Partnerships Program.