Open Arduino IDE and write code

How it's done

Write following code in Arduino IDE:

int floatswitch = 7;

void setup() {
pinMode(floatswitch,INPUT_PULLUP);
Serial.begin(9600);
}

void loop() {
 int x = digitalRead(floatswitch);
 Serial.println(x);
}

Extra info:

Code explanation:

int floatswitch = 7; //Sensor pin define which pin we use to read sensor value.

pinMode(floatswitch,INPUT_PULLUP); // Define that sensor pin will be used as input (to read) with built-in pullup resistor. On this way we avoid to use additional resistor.

Serial.begin(9600); // Allow us to read data on serial monitor.

int x = digitalRead(floatswitch); // Declare integer variable x and initialize it with digital read value from sensor.

Serial.println(x); // Print value of x variable in serial monitor with line break.

Activity Q&A

Need help with this activity?Add the first post