This lab will introduce you to working with sensors and the Arduino. Also, you will use the serial monitor.
The float sensor is a fairly easy sensor to work with. One pin of the sensor leads should be grounded. Plug the other one into digital I/O #2.
Secure the float sensor so that it hangs vertically. It does not have to be immersed in liquid for this experiment. (For safety reasons, it is a good idea NOT to immerse it in liquid.)
Load the floatSensor code, connect the Arduino to your computer via the USB cable, then compile ("verify") and "upload" the code. Click on the "Serial Monitor" icon. Below is the code. You could download it using the link above, or copy and paste it.
/*Make sure the serial communication baud rate is correct. What you see on the serial monitor should match the value in the "Serial.begin" line of the code.
You should see "Float sensor" appear, followed by a value. After a few cycles, hold up the float cylinder at the bottom of the sensor. You should see the value change on the serial monitor. After a few cycles, let go. You should see the value change back to the original value.
Change the program to turn on the LED (from the first Arduino tutorial) when the float sensor indicates that the cylinder is in the upper (vertical) position. If the cylinder is in the lower position, the LED should be turned off.
So far, we have seen the
Serial.print
command used to send information from the Arduino. Now include the
Serial.read
command to send information to the Arduino.
Here is some example code. It sets a Boolean value called "lightLEDwhenOn"
to true or false, depending on whether the user enters
b or
B.
Include this code, and light the LED only when "lightLEDwhenOn"
is true and the cylinder is in the upper (vertical) position.
char val = Serial.read();
if (val != -1) {
switch (val) {
case 'b' :
lightLEDwhenOn = true;
break;
case 'B' :
lightLEDwhenOn = false;
break;
}
}
Follow the directions on Lab Report format, given on the class web-page. Write-up what you did IN YOUR OWN WORDS. Do not simply re-word the directions, but explain what is going on. Avoid listing steps. Use outside sources as appropriate, and be certain to cite your sources. If you copy and paste ANY text, put it in double-quotes and include a citation immediately after it.
Please see the link to the lab format.