CSc 4110/6110 Embedded Systems
Lab Experiment #2
Instructor: Dr. Michael Weeks

Working with a float sensor

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.

/*

Read a float sensor.
by Michael Weeks, 2012-2013

*/

const int floatSensorPin = 2;

void setup() {
      Serial.begin(9600);
      // Make the floatSensorPin an input pin.
      pinMode(floatSensorPin, INPUT);
}

void loop()
{
      digitalWrite(floatSensorPin, HIGH);
      delay(100);
      Serial.print("Float sensor: ");
      Serial.println(digitalRead(floatSensorPin));
      delay(2000);
}

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;
    }
  }

Questions

  1. What value was reported initially? What value was reported when you held up the float cylinder?
  2. If you were to mount the temperature float sensor upside-down, how would that have affected this experiment?
  3. What is the purpose of the digitalWrite command in the code? What would happen if it wrote a LOW value instead?
  4. What would you have to do to make this work with digital I/O pin 3 instead of pin 2?
  5. What if we reversed the leads of the sensor? Would it still work?
  6. Document the code in the "loop" section.


What to Turn In

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.