CR102 Tutorial – Class 2

Welcome to the second tutorial for CR102. This lesson will cover the serial monitor, the ultrasonic sensor and number comparison.

The Serial Monitor

The serial monitor is a feature built into the Arduino development environment that allows us to communicate by sending and receiving data.

This is different from when you upload programs to the Arduino board; the data we see in the serial monitor tells us what’s happening while we run the program. The reason why we want to do this is so that we can use the information from serial monitor to find out the exact problem when a program isn’t working the way we want it to.

CR102 Tutorial - Class 2

We can open the serial monitor by pressing the button next to Upload to Arduino on Cardublock.

Project 1: Using the Serial Monitor

CR102 Tutorial - Class 2

1. We will need to use a new bin – the white one labeled Communication. From there, drag a Serial Print block into your loop.

CR102 Tutorial - Class 2

2. Write the message you want the serial monitor to show in the block that says “message2” next to it.

3. Add a short delay under the Serial Print block. Half a second should be fine.

4. Upload your program and open the serial monitor. After a second or two, you should start seeing the message you placed in the serial print block being repeatedly displayed in the serial monitor.

Tip: The serial monitor is a great way to test if input devices are working!

The Ultrasonic Sensor

A CAROBOT SwissCHEESE ultrasonic sensor.

An ultrasonic sensor is a type of input devices that uses sound to sense the distance between itself and an object. One of the two speaker-like objects on the sensor produces sound waves which bounce off of surfaces, while the other receives the reflected sound waves. By using the time taken for the sound waves to return, the sensor can determine how far an object is. It’s effectively an artificial version of the echolocation that animals like bats and whales use to “see”.

CR102 Tutorial - Class 2

It’s important to understand that some objects might not be detected by ultrasonic sensors. This often occurs if the material doesn’t reflect sound well or if the object is shaped strangely.

Project 2: Using the Ultrasonic Sensor

Always disconnect the power (USB cable) when wiring electronics!

1. You will need your code from the previous project.

2. There are two pins for wires on the sensor: one labeled TRIG and the other labeled ECHO. Using wires, plug the TRIG wire into O0 and the ECHO wire into I0.

CR102 Tutorial - Class 2

3. Back in CarduBlock, go into the CAROBOT SwissCHEESE bin and drag an Ultrasonic block into the sketch.

4. From the Communication bin, drag out the first glue block. Replace the message in your serial print block with the glue block. This will act as an adapter of sorts for the ultrasonic sensor.

CR102 Tutorial - Class 2

5. Put the Ultrasonic block into the glue block. Instead of printing a message, the serial monitor should now print the feedback from your sensor.

CR102 Tutorial - Class 2

6. Upload your code and open the serial monitor. If you did this correctly, you should start seeing numbers on the serial monitor. These should change as you point the sensor at closer or farther objects. Note that the distance values are in centimeters.

Number Comparison

CR102 Tutorial - Class 2

When we are dealing with two numbers, we need a way to know which one is bigger and which one is smaller. For a program to do that, we’ll need to use some number comparison tools. They can be found under the orange Tests bin, although we’ll only need two of them for this course: greater than (>) and less than (<).

CR102 Tutorial - Class 2

Project 3: The Ultrasonic Sensor with Number Comparison

The goal of this project is to program a light that will only turn on if the ultrasonic sensor is within a certain distance of an object.

Always disconnect the power (USB cable) when wiring electronics!

1. Plug an LED into your board.

2. Drag a set integer variable block into the loop from the Variables/Constants bin. Rename the variable to something meaningful like “Distance”.

3. Delete the value and replace it with an Ultrasonic block.

CR102 Tutorial - Class 2

4. Put an if/else statement under the variable. Put a greater than block into the test section.

5. Get a variable block (the actual variable, not the block to declare a variable) from the Variables/Constants bin. Put it into the first slot of the comparison block.

CR102 Tutorial - Class 2

6. Find a integer constant block from the same bin. It should look like a small pink hexagon with a 1 in it. Drag it into the second slot of the comparison block, and change it to 300. The comparison block should now read “Distance > 300”

7. Think about what you want the program to do. If the sensor sees the distance is greater than 3 meters, what will it do? If it sees a closer object, what will it do?

8. The rest is yours to discover!

CHALLENGE: Build a machine using mechanical parts. This machine will sense if something is near it, and attempt to hit the object with an arm.

That’s all for this lesson! Stay tuned for the third one!