# Raindrop Sensor with micro:bit

#### Step 1   The Module

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbFRQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--412971d45c854bc21a3ef91e361bedeb7641d97d/step_760_img1.png)

* Let's take a closer look at the Raindrop Sensor Module! There are four pins at the bottom:

  3.3V  : 'VCC' stands for Voltage Common Collector, we'll connect the VCC pin to 3.3V on the micro:bit

  GND: In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called  ground or GND.

  Digital Output (DO)

  Analog Output (AO)
* Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to.&#x20;
* 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

```
let raindrop_analog_value = 0
basic.forever(function () {
    raindrop_analog_value = pins.analogReadPin(AnalogPin.P1)
    if (raindrop_analog_value < 300) {
        basic.showNumber(raindrop_analog_value)
        pins.digitalWritePin(DigitalPin.P2, 1)
        basic.pause(500)
        pins.digitalWritePin(DigitalPin.P2, 0)
        basic.pause(500)
    } else {
        basic.showNumber(raindrop_analog_value)
        pins.digitalWritePin(DigitalPin.P2, 0)
    }
})
```

* First, let's open up the MakeCode editor and start a new project.&#x20;
* Click on the 'New Project' button
* Add this code to the Javascript interface.

#### Step 13   MakeCode using two forever loops

```
let raindrop_analog_value = 0
basic.forever(function () {
    raindrop_analog_value = pins.analogReadPin(AnalogPin.P0)
    basic.showNumber(raindrop_analog_value)
})
basic.forever(function () {
    if (raindrop_analog_value < 300) {
        pins.digitalWritePin(DigitalPin.P2, 1)
        basic.pause(500)
    }
    pins.digitalWritePin(DigitalPin.P2, 0)
    basic.pause(500)
})
```

* 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.&#x20;

  Replace the existing code with this in the Javascript interface.

#### Step 14   Upload the code

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbFhQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--29ed98b6e8faba201cd18194780a4372a60cc29e/step_773_img1.png)

* Alright, now let's upload the hex file to the micro:bit. Connect the micro:bit to your computer using a microUSB cable
* Click on the Download button on the bottom left-hand corner of MakeCode editor<br>
* Find the hex file <br>
* Open up Finder on the MacOS or Explorer on Windows, and drag the hex file into MICROBIT under 'Devices' on the macOS.
* The micro:bit will flash for a few seconds while the code uploads<br>

#### Step 15   Start the Arduino IDE

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbGJQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--c17af1e5caad02cc0c974e7e5fa6611c0041a04d/step_774_img1.png)

* 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   Upload the sketch

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbGZQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--56f555c8497a06c80d88873b23d6082f498c10b4/step_776_img1.png)

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbGZQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--56f555c8497a06c80d88873b23d6082f498c10b4/step_776_img1.png)

![](https://littlebirdelectronics.com.au/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBbGpQIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e57fc3dc5f332e624936bbae1afbb2b0d5a63efc/step_776_img2.png)

* 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<br>

#### Step 17   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");
 }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.linhkientot.vn/micro-bit-products/micro-bit-starter-kit-v1/raindrop-sensor-with-micro-bit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
