FINAL Project02 – The Use of Rainwater

Introduction of OLED I2C Display1891524023223_.pic.jpg

How to connect and program the Geekcreit 0.96 inch 4 pin white I2C OLED module with Arduino. OLED display based on a SSD1306 OLED driver IC.

The display connects to Arduino using only four wires – two for power and two for data, making the wiring very simple. The data connection is I2C (I²C, IIC or Inter-Integrated Circuit). This interface is sometimes called TWI (Two Wire Interface).

Reference from:×64-I2C-display/

Testing – 1 Display Text on OLED Screen






Connect the display to UNO board as the image shown.

Before start coding, I need to import two zip libraries for add graphic to the display.

屏幕快照 2018-04-18 04.50.39



















After reading the reviews of this module from Amazon, I’ve done some changes to the raw files, and started coding on a sketch template from×64-I2C-display/

Here’s the code.

屏幕快照 2018-04-18 05.08.50.png

This is how it shows on the display.


Testing – 2   Display Sensor Data on the Screen


Add a water level sensor to the circuit on pin A0.

A Flow Chart for Programing



屏幕快照 2018-04-18 05.02.36.png

Then add these code before setup as a intro for the water level sensor.
屏幕快照 2018-04-18 05.03.53.png




Also add these to loop to run the program, so the reading will be taken from pin A0 and displayed on the screen.




Here’s the video show how it works.

And this is the full code for this part.


As I’ve make the valve and OLED display run perfectly, then I put them into one circuit, and merged the code together.


The Full Version Flow Chart


The full code for this project is here.

Prototype Building


Here’s the video for the working prototype.

The model will leak water, so I use finger instead of water to show how it works.




WEEK 9 Project02 – The Use of Rainwater

How to use water level sensor to control a servo?


Aim to fill the toilet tank by rainwater by using the non-contact liquid sensor to control the valve. When the toilet tank is empty, the servo will rotate to open the valve for rainwater filling, and it will rotate back to the original position as closing the valve when the tank is filled up.

Testing – 1  Make the Servo Rotate in Clockwise and Anti-clockwise

屏幕快照 2018-04-18 02.45.22.png

Connect the circuit as above, then upload the “sweep” code for servo testing.

屏幕快照 2018-04-18 00.35.04.png



Testing – 2   Use Non-contact Liquid Sensor to Control Servo


Add a non-contact liquid sensor at the servo testing circuit on digital pin2, then add the sensor sketch to the servo testing code and upload it.

A Flow Chart for Programing


Full code:

屏幕快照 2018-04-18 03.34.07.png


This video shows how it works when it’s attached to the prototype, so the valve will open when there is no water detected from the toilet tank, and it will keep shut if water is detected. ( The model will leak water, so I use finger instead of water to show how it works.)

Next Step

I’m going to use a OLED I2C display to read the water level sensor in the rainwater tank for people to monitor the water level change .

WEEK 8 Project02 – The Use of Rainwater

Introduction of Reading Sensor Value on Blynk App

How Blynk Works

Blynk was designed for the Internet of Things. It can control hardware remotely, it can display sensor data, it can store data, vizualize it and do many other cool things.

There are three major components in the platform:

  • Blynk App – allows to you create amazing interfaces for your projects using various widgets we provide.
  • Blynk Server – responsible for all the communications between the smartphone and hardware. You can use our Blynk Cloud or run your private Blynk server locally. It’s open-source, could easily handle thousands of devices and can even be launched on a Raspberry Pi.
  • Blynk Libraries – for all the popular hardware platforms – enable communication with the server and process all the incoming and outcoming commands.



  • Similar API & UI for all supported hardware & devices
  • Connection to the cloud using:
    • WiFi
    • Bluetooth and BLE
    • Ethernet
    • USB (Serial)
    • GSM
  • Set of easy-to-use Widgets
  • Direct pin manipulation with no code writing
  • Easy to integrate and add new functionality using virtual pins
  • History data monitoring via History Graph widget
  • Device-to-Device communication using Bridge Widget
  • Sending emails, tweets, push notifications, etc.


Introduction of Arduino Ethernet Shield W5100


The Arduino Wiznet Ethernet W5100 Shield allows an Arduino board to connect to the internet. It is based on the Wiznet W5100 ethernet chip providing a network (IP) stack capable of both TCP and UDP. The Arduino Ethernet Shield supports up to four simultaneous socket connections. Use the Ethernet library to write sketches which connect to the internet using the shield.



From the tutorial provided by Blynk website :

I connected the shield to UNO board, and got a sketch example from:


屏幕快照 2018-04-13 04.08.23.png














However I can get the IP address, but couldn’t connect to the blynk cloud web.














Introduction of ESP-01 / ESP8266 Wifi module

As the ethernet shield is not working, I decided to try the small wifi chip to make the Arduino UNO connect to the internet.



The ESP8266 WiFi Module is a self contained SOC with integrated TCP/IP protocol stack that can give any micro controller access to your WiFi network. The ESP8266 is capable of either hosting an application or offloading all Wi-Fi networking functions from another application processor.



I Added an adapter to this module and connect it as below.


屏幕快照 2018-04-13 04.54.48.png

I got a sketch example from:

However the network kept showing “not responding”, it doesn’t work as well. Probably because the module wasn’t in the latest firmware, it should be flashed.

Next Step

As reading the sensor on App seems quite difficult to come true, I ‘m planning on reading sensor data on a LCD or OLED display.






WEEK 7 Project02 – The Use of Rainwater

Introduce Water Level Sensor

NO.1 Testing


Water level sensor is a module that can sense the depth of water, whose core part is an amplification circuit composed of a transistor and several comb-shape PCB cables. When placed in water, the comb-shape cable will change its resistance with the depth of the water and convert the depth signal into an electrical signal. With an ADC on the SunFounder Uno board dealing with that signal, we can know the water depth changes.


屏幕快照 2018-04-12 21.13.03.png

Connect the circuit like above. Connect the sensor to A0, and led to pin3.

屏幕快照 2018-04-12 21.14.22.png

Select the AnalogInOutSerial code in Example, and make sure the Output pin is correct.

Reference from:



NO.2 Non-contact Liquid Level Sensor


The non-contact liquid level sensor utilizes advanced signal processing technology by using a powerful chip with high-speed operation capacity to achieve non-contact liquid level detection. No contact with liquid makes the module suitable for hazardous applications such as detecting toxic substances, strong acid, strong alkali and all kinds of liquid in an airtight container under high pressure. There are no special requirements for the liquid or container and the sensor is easy to use and easy to install.
The liquid level sensor is equipped with an interface adapter that makes it compatible with DFRobot “Gravity” interface. There are 4 levels of sensitivity which are set by pressing the SET button.



Using the same connection and code as the previous one to test the non-contact liquid sensor.


Next Step

I’m going to work out how to use water level sensor to control a servo for valve opening and closing, also have a look at how to read sensor data on a smart phone App.

WEEK 6 Project02 – The Use of Rainwater

Project Aim

By collecting rainwater for home using to reduce the water consumption and make our lives more sustainable.

Initial Idea


To create a smart toilet flushing system by using collected rainwater. When the toilet water tank is empty, the Valve1 will open to let rainwater flush the toilet. If the rainwater tank is empty as well, the Valve2 will open to use city tap water to flush.

Developing Concept


Using one water level sensor in toilet tank to control the valve in the rainwater tank for opening and closing, and using smart phone app to observe the volume of the water in the tank by reading the value from the sensor in the rainwater tank.

Component needed:

Arduino UNO Board


Water Level Sensor x2


Wifi Shield / Wifi Module / Bluetooth Module

Smart Phone App


WEEK 5 Project02 – The Use of Rainwater



Water consumption is always a big problem for our human in nowadays. How to use water efficiently in our lives should be an important aim for everyone. Therefore it reminds me that we can use rainwater to reduce the pressure of city water supply. Rainwater itself is generally clean, but it can pick up microorganisms, pollutants and debris when it hits the roof. This is why systems for rainwater use inside home often include filtration or other treatments for safety.

Uses for Rainwater

Flushing Toilets

This is another huge water drain. Toilets use almost 27 percent of water in your home. To use collected rainwater instead, try keeping a bucket of it next to your toilet. When you need to flush, pour the rainwater straight into the bowl of the toilet. This will automatically flush your toilet. Make sure your bucket can hold the amount of your toilet’s tank. For instance, if you have a toilet with a 6 gallon (22.7 liter) tank, use at least a 6 gallon bucket of water

Another option is to plumb a pipe for rainwater directly into your house and connect it to your toilet for flushing.

Watering Lawns, Gardens and Houseplants

Rainwater is naturally designed to water plants, and it can easily be used for your indoor and outdoor gardens. You can use rainwater in watering cans to water plants by hand. You can also attach any rainwater storage tanks directly to an automatic irrigation system.

Passive systems to conserve and collect water in your soil are also helpful. Plant garden beds along the edges of your driveway, or at the bottom of a hill, to take advantage of water’s natural movement. Also, try planting a rain garden at the ends of your eavestroughs to catch any excess runoff.

Rinsing Vegetables

Dirty rainwater is great for rinsing vegetables straight from your garden, especially root vegetables. Try filling a large bucket with rainwater, adding some carrots, potatoes, beets or other hard vegetables, and swish them together to knock the soil off.

Washing Vehicles and Equipment

Washing outdoor items is another excellent use for untreated rainwater. Cars, garden tools, lawnmowers, tractors and even the driveway and sides of your house are all perfect candidates.

Reference from:

Rainwater harvesting

Rainwater from roofs and hard surfaces such as car parks can be stored and used in and around properties. The simple rainwater butt, used for garden watering, is a familiar method of storage.

There has been a recent growth in the use of the collected water for a range of non-potable uses, particularly for flushing toilets. Stored water is generally held in a suitably sized underground tank and pumped to the point of use. A mains water supply is usually provided as a back-up if rainwater is not available.


Reference from:

WEEK 4 – Individual Project 01- MusiCake

Project Aim

I’ve done the testing of flame sensor and stepper motor at the previous stage, and the aim for this stage is to run these two components at the same time. The motor, led lights and buzzer will be on when flame is detected, and they will stop when the flame goes out.

Components needed:

Flame sensor


Led lights

Stepper Motor



Circuit Diagram

屏幕快照 2018-02-25 02.17.04.png

I connected the stepper motor to the Digital Pin 8 to ~11, the buzzer to digital pin ~5, the red led light to pin ~6, the green led light to pin 7. And I connect the AO pin of the sensor to pin A2, the DO pin to the pin ~3.

Schematic Diagram

屏幕快照 2018-02-25 02.32.14.png

Task 1 –  How to use flame sensor to control the stepper motor?


屏幕快照 2018-02-25 20.37.16.png





I put stepper motor speed into Setup { } with other component set up.




And, I put the direction of motor rotate under the ” if ( val == HIGH) “, which means if flame is detected by the sensor, the motor will start to run.


屏幕快照 2018-02-25 20.25.21.png


These code is for birthday song playing by the buzzer.

These code is for birthday song playing by the buzzer.

After upload this code to Arduino, the stepper motor will run when flame is detected, however, the led lights and buzzer only can run after the stepper finish one rotation. Also, the lights and buzzer won’t stop running if the flame goes out already, they only stop until the birthday song is finished. This situation is not what I want.

Please check the video to see the problem.


Task 2 – How to run stepper motor, led lights and buzzer at the same time when flame is detected?

Final flowchart.png

The flowchart shows the overall structure of the code should be.

As the myStepper.set function is to control a certain number of step, the next instruction cannot be executed before the motor finish one step, so the led lights and buzzer will run afterward.

final code-1.png
final code-2.png

To solve the problem, I set the ” stepsPerRev = 128 ” and the speed = 200.



Then, I insert ” myStepper.step(stepPerRev) ” after every set of output instruction instead of “delay( )”, so that the motor will run with other components at the same time.

The birthday song has 4 parts, this is a small part of part one.





final code-3.pngAlso, I set every 3  tone as a group in every “if(val=HIGH)” loop, which aims to make sure the program will check if there is flame detected after 3 tone produced, so that the program can stop when flame goes out.




The complete code is here.

Please check the video for final working model.




Flame sensor coding reference from:


WEEK 3 – Individual Project 01- MusiCake

Project Brief

This project is to break the common sense about a flame sensor not only can be used on fire alarm but also can be applied to many other creative projects.


The idea is about using the flame sensor to control the stepper motor, buzzer and LED lights to build a creative music box which can put a cake on it. The buzzer will play the music when flame is detected, and music stop when flame goes out. And, the motor will start to rotate the cake when flame is detected.

TASK 1 – How to use flame sensor to control a buzzer and a LED light at the same time?


Flame sensor

LED light



屏幕快照 2018-02-23 23.18.04.png

I connected the AO pin of the sensor to A0 input pin on UNO board, the LED light goes to Digital ~9, and the buzzer goes to Digital 12.


Schematic Diagram

屏幕快照 2018-02-25 02.41.58.png


sensor testing flowchart.png


Reference from:

屏幕快照 2018-02-25 05.52.26.png

I tried to add a tone for the buzzer. The “tones[] = (525)” means the buzzer will produce sound in 525Hz.

This is a video I’ve done about how it works.


Task 2 – How to drive a stepper motor?

屏幕快照 2018-02-24 00.22.38.png

Connect the stepper motor to the UNO board as above.



屏幕快照 2018-02-24 00.45.16.png

This code I’ve done during the tutorial session is for running the stepper motor.


WEEK 2 – Individual Research – Flame Sensor

How does flame sensor work?

The flame sensor has both outputs, analog and digital. The analog output gives us a real-time voltage output signal on thermal resistance while the digital output allows us to set a threshold via a potentiometer.  We can use the flame sensor to make an alarm when detecting the fire, for safety purpose in many projects and in many more ways.

Flame Sensor Interfacing with Arduino Using Digital Pin

屏幕快照 2018-02-23 19.40.44.png

First, connect the + and the GND pin of the sensor to the +5V and the GND of the Arduino. Then connect the DO pin to the Digital Pin 2 of the Arduino. Next, I connect the led at the pin 12 of the Arduino. When the sensor will detect a flame, then the led will light up.


Code for Flame Detection Using Digital Pin

digital pin.png

Flame Sensor Interfacing with Arduino Using Analog Pin

屏幕快照 2018-02-23 19.58.25.png

When using the analog input of the Arduino. The Arduino will read from the sensor and gives us values from 0 to 1023. The only difference is to connect the AO pin to the Analog Input A0 pin on Arduino.


Code for Flame Detection Using Analog Pin

analog pin.png

Reference from:

WEEK 1 – Individual Research – Flame Sensor

What is flame sensor?


The flame sensor is used to detect the fire or other infra red light which are in the range of wavelength from 760nm to 1100nm. The module consists of an IR sensor, potentiometer, OP-Amp circuitry and a LED indicator. When a flame will be detected, the module will turn on its red led. This module is sensitive to flame but it can also detect ordinary light. The detection point is 60 degrees. The sensitivity of this sensor is adjustable and it also has a stable performance.


What does flame sensor have?

sensor intro.jpg

A0: This is the analog pin and this will be connected to the analog input of the Arduino.

G: This is the ground pin and this will be connected to the ground of the Arduino.

+: This is the input voltage pin of the sensor and this will be connected to the +5V of Arduino.

D0: This is the digital pin and this will be connected to the digital input of Arduino

Flame Sensor Circuit Diagram


Circuit diagram Source:

The flame sensor module has the following features.

  • The operating voltage is from 3.3 – 5V.
  • It gives us both analog and digital output.
  • It has a led indicator, which indicates that whether the flame is detected or not.
  • The threshold value can be changes by rotating the top of potentiometer.
  • Flame detection distance, lighter flame test can be triggered within 0.8m, if the intensity of flame is high, the detection distance will be increased.
  • The detection angle of the flame sensor module is about 60 degrees.Reference From: