mBlock for Arduino Mega project (3) tilt switch sensor project

Youth Innovation Lab
4 min readMar 18, 2020

--

After the first two simple Arduino Mega project — blink LED on Mega board, and light sensor controlled LED project, now, we can continue to build another relatively simple project, this time, we will use a not very popular sensor, tilt switch sensor.

Tilt sensors allow you to detect orientation or inclination. They are small, inexpensive, low-power and easy-to-use. If used properly, they will not wear out. Their simplicitiy makes them popular for toys, gadgets and appliances. Sometimes they are referred to as “mercury switches”, “tilt switches” or “rolling ball sensors” for obvious reasons.

They are usually made by a cavity of some sort (cylindrical is popular, although not always) and a conductive free mass inside, such as a blob of mercury or rolling ball. One end of the cavity has two conductive elements (poles). When the sensor is oriented so that that end is downwards, the mass rolls onto the poles and shorts them, acting as a switch throw.

Make sure that you understand, its on and off is depends on the position of the mercury ball, see above sample picture.

Before we start our light sensor project, we will try it at the Arduino IDE first, it is the typical proof of concept step, so we know how to create the right project with the right result.

int delayVal = 500;
int debug_values = 1;

void setup() {
Serial.begin(9600);
Serial.println(“Read Tilt Sensor”);
}

void loop() {
while (digitalRead(7) == HIGH) {
pinHigh();
if (debug_values) {
Serial.println(“PIN HIGH”);
}
delay(delayVal);
}
pinLow();
if (debug_values) {
Serial.println(“PIN LOW”);
}
//delay
delay(delayVal);
}

//Custom functions
void pinHigh() {
digitalWrite(13, HIGH);
}

void pinLow() {
digitalWrite(13, HIGH);

}

This code is very straightforward, in setup(), initialize the digital pin as an output mode, and open serial port connection (for debugging), then in loop(), read the light sensor value, turn the LED on (HIGH is the voltage level) if the value above the threshold (predefined) , or turn the LED off by making the voltage LOW if the value below the threshold (predefined).

Remember to set the right board (Arduino Mega) and port (port 7 in my test case), then choose “upload” (under Sketch menu), you can start to observe the result, the blink LED on board.

Remember to start the Serial Monitor (from “Tools” menu), and observe the change of the light sensor reading.

Now, it is the time to turn this Arduino project into a mBlock project.

Step 1: start mBlock 5, under “Devices”, click “+”, and choose the right device (Arduino Mega for our project), click “OK”.

step 2: hook Arduino Mega (or UNO) board to one of your USB interface, choose “Upload mode” on, and click “connect” to connect the Mega board.

Hint: remember to choose the right port (for multiple ports)

step 3: from category, choose the right category, then start to drag and drop the blocks, build the project, and save it in your computer (I prefer to save it on local computer, you can choose to save it on cloud only, that is up to you).

Now, it is the time to test it, it may be the simplest project we can build using mBlock for Arduino Mega board, and no need any extra sensors, give it a try.

Please add your comments or suggestions, we will post more tutorials soon.

--

--

Youth Innovation Lab
Youth Innovation Lab

Written by Youth Innovation Lab

Youth Innovation Lab is community based STEM education institute, we promote STEM education to minority community, and focus on advanced technology education.

No responses yet