Raindrop Sensor with micro:bit

Difficulty
Easy
Steps
17

A raindrop module is a useful little tool for detecting when it rains and also for measuring rainfall intensity.

In this guide, we will connect a raindrop sensor module to the micro:bit and prototype a rain detection device!

Finish this guide and get started with creating your own rain detection device. 

Parts Used in This Guide

Buy

Step 1  The Module

Let's take a closer look at the Raindrop Sensor Module!

There are four pins at the bottom:

  • Voltage Common Collector (VCC)
  • Ground (GND)
  • Digital Output (DO)
  • Analog Output (AO)

There are also two pins at the top that connect to the second part of the raindrop sensor module. One end connects to ground and has a symbol next to it, that looks like a horizontal version of ⏚

Step 2  Connect module to breadboard

Step 3  Connect 3.3V to VCC

Step 4  Connect GND to GND

Step 5  Connect P0 to DO

Step 6  Connect P1 to AO

Step 7  Connect GND to LED (Negative lead)

Step 8  Add a resistor

Step 9  Connect P2 to resistor

Step 10  Connect Raindrop Module - GND

Step 11  Connect Raindrop Module - +

Step 12  MakeCode using one forever loop!

  • First, let's open up the MakeCode editor and start a new project. 
  • Now create a new variable, "raindrop analog value" and set it to read Pin 1. Because it is connected to AO (Analog output) of the raindrop sensor board, this will be an 'analog read pin' block.
  • Create an 'if ... then ... else' block, set a condition: 'raindrop analog value < 300' 
  • So, if the analog value of the raindrop sensor is less than 300, we will make the LED blink. This is done by using 'digital write pin P2 to 1' block and 'digital write pin P2 to 0' block. 
  • Else, the LED will not blink, and we simply use one 'digital write pin P2 to 0' block.
  • Let us also see the analog values by using "show number" block in either case.

Step 13  MakeCode using two forever loops!

If you uploaded the previous MakeCode now, it will first display the analog value and then if the value is less than 300, the LED lights up.

But what if you wanted there to be no delay between the number showing, and the LED lighting up?

You can use two forever loops to do just that. 

Get this MakeCode by pressing the download button below!

Step 14  Upload the MakeCode

Alright, now let's upload the hex file to the micro:bit.

  1. Connect the micro:bit to your computer using a microUSB cable
  2. Click on the Download button on the bottom left-hand corner of MakeCode editor
  3. Find the hex file 
  4.  Open up Finder on the MacOS or Explorer on Windows, and drag the hex file into MICROBIT under 'Devices' on the macOS.
  5. The micro:bit will flash for a few seconds while the code uploads

Step 15  Start the Arduino IDE

Optionally, the Micro:bit can also be programmed using the Arduino IDE. If you haven't got the Arduino IDE already installed, please refer to our previous guide on how to set up the Arduino IDE to program the micro:bit.

Step 16  The code


              int Sensor = 1;  //Raindrop sensor
int Led    = 2;  // LED
void setup()
{
 pinMode(Led, OUTPUT);
 pinMode(Sensor, INPUT);
 Serial.begin(9600);
 }
void loop()
{
 int temp = digitalRead(Sensor); // Read Digital value from raindrop sensor
 if (temp == LOW)
 {
   digitalWrite(Led, HIGH); // LED-----> HIGH
   Serial.println("It's Raining!");
 }
 else
 {
   digitalWrite(Led, LOW); // LED----->LOW
   Serial.println("Not Raining");
 }
}
            
  • We create two variables which will be integer values, 'Sensor' and 'Led'. 
    • The sensor pin is connected to Pin 1
    • The positive leg of the LED is connected to Pin 2
  • Then in the setup loop, we define 'Led' as an output value and 'Sensor' as an input value. 
  • Serial.begin  starts the data rate in bits per second (baud) for serial data transmission. Because we are communicating with the computer, we use the rate '9600'. However, there are other rates you can use. You can read more about it here in the Arduino documentation on begin() 
  • Now in the loop that goes on forever until the micro:bit is turned on, we specify that the variable which contains an integer, 'temp' contains the digital value from the raindrop sensor.
  • If 'temp' is LOW or '0', we turn the LED on.
  • Else, if 'temp' is HIGH, the LED does not light up.
  • We will also use 'Serial.println' 
    • to print 'It's Raining!' to the console when temp is LOW
    • or 'Not Raining' when temp is HIGH

You might be wondering why if 'temp' is LOW, then the LED will light up. This is because we have a digital output pin that is an active-low, so the signal will only perform its function when its logic level is LOW or '0'. The function, here, is to turn the LED on.

Step 17  Upload the sketch

  • Click on the check icon on the top left hand-corner to verify the code
  • Click on the upload icon next to it to upload the sketch