This week's assignment is to design and build a smart gadget/device that fits one of two themes either a smart device for backpack or smart device for work desk. I chose to make a device for my work desk. I have decided to make a smart hydration reminder. As a person who forgets himself while working, I need to remind myself to stay alive and hydrated 😌.
Software: Tinkercad
Software:
Arduino IDE
Components: Arduino Uno board
Components: Potentiometer
Components: LCD with I2C module
Components: IR sensor
Components: Buzzer
Components: Push button
Materials&Tools: Cardboard
Components: Mini breadboard
Components: Jumper wires
Materials&Tools: Cutter
Materials&Tools: Glue gun
Materials&Tools: Pen and ruler
Circuit connection on tinker cad
Circuit design:
Link for circuit: Here
On Tinkercad, I started to put the components I will use in my circuit.
I put the Arduino board and breadboard.
Mainly, the circuit consists of two action components (OUTPUT) which are the LCD screen and the buzzer, so I added them.
Three inputs are used. An IR sensor is added as an input signal for the circuit, a potentiometer, and a push button (INPUT).
I used 5v from arduino to power the circuit.
I connected 5V and GND in arduino with +ve and -ve rails on the breadboard.
I connected the buzzer on analog output pin 11 with +ve terminal and GND with -ve terminal.
Then, I connected the LCD screen after reviewing the pinout from midweek tutorials. It is connected with arduino using 4 pins, VCC, GND, SDA and SCL. I connectd them to A4 & A5.
The IR sensor is connected on pin 4 and +ve and -ve with rails to arduino power.
The potentiometer is connected to analog input pin A3 to select the interval which the timer will work for.
The push button is connected on pin 3 to start the timer and the other terminal to GND.
Using examples as guide to write the button state detection
Code for variables & setup function
Code & logic:
I started first by dividing the problem and defining the objectives clearly and what I want to achieve.
As I want to make the push button trigger an action every time it is pressed and not only when holded down, I searched how to do so and I found several ways but the state change detection example was the easiest one to edit.
It is simply done by using two variables, current button state and previous button state. The current one stores the readings from the button pin and at the end of the void loop the value is stored in the previous button state variable.
By comparing the values of these variables, I can simply detect the current state of the button if it is pressed or not.
I thought what is the right condition I should write and decided to make it like the following: If (btnState is HIGH and prevBtnState is LOW) => triggers the motors motion.
This means when the button is pressed and before that it wasn't pressed. This technique is used mainly to prevent the repetition of the action while the button is held down.
To use the LCD, I imported the <LiquidCrystal_I2c.h> library and edited the size to be 16x2 like the one I have.
I declared a variable for component's pin. Two Variables to use with millis() function (current and previous millis) and two variables which are used as flags to control flow.
In setup function, I wrote the pinMode for every component and initialized the lcd and printed "place the bottole" as the first thing to appear because placing bottle is what will initiate program flow.
In the void loop() function, I declared 3 dynamic variables to store input componenets readings. Two digital inputs which are "irReading" (for IR sensor) and "btnState" (for push button) and one analog input which is "interval" that is used to store potentiometer input.
I used the map fucntion to scale the input values (0, 1023) to time interval from 5 sec to 30 sec. After that I divided it to ranges and each range corresponds to a letter . "S" for short, "M" for medium and "L" for long using an if-else.
For the timer, I used the function millis() which is a function that returns the number of milliseconds since the start of the program and this happens every iteration to keep track of current time like a clock.
The program starts to operate when IR sensor reads LOW which means it detects a bottle and if so, it asks the user the adjust potentiometer to choose interval [S/M/L - printed top right on LCD] and press the button to start .
This happens when timer is not active and reminder is not active. That means when timer hasn't been started and reminder hasn't been triggered.
When button is clicked, the startTimer() function is called. This funciton simply does 3 things:
Prints "timer is running"
Set timerActive flag to true
Save a time stamp in prevMillis to indicate when timer started.
This is the key point in using millis() logic, because later on, the difference between current and previous indicates if some period of time has passed.
Code of loop function & user interface
Body of startTimer function [outside loop()]
Code of loop function & trigger of reminder and reset
Body of reminderTone function [outside loop()]
Only when the timer is active, the main condition is checked to trigger the reminder. If sensor reads LOW (bottle exists) and the specified interval passed, the drink statements is printed and buzzer works. In addition, the flags are updated.
When the reminder is acitve, the reset action is needed. Resetting is done by removing the bottle (irReading is HIGH) and replacing it again (irReading is LOW) which means the user has drinked.
While reminder is active and the bottle is not removed the drink statement still apperars. If removed, condition is met and “bottle removed” is printed and reminder is reset.
When it is replaced, the whole program starts again at the point "adjust interval and press” because the two flags are now false (initial state).
The reminderTone function consists of a for loop to play buzzer ringtone 3 times which is set using the tone(pin, frequency, duration) function.
Simulation on Tinkercad to test the circuit
Circuit Implementation and mounting:
I set the dimensions of the cardboard based on LCD dimensions, arduino and minimized it so that it takes less space on desk.
I cut the parts of the body, top face with window for LCD , 4 sides and bottle base. Then I assembled them using glue gun.
After that, I started to connect the circuit on the mini breadboard:
I connected the buzzer on analog pin 11 and other leg to GND on arduino directly.
I connected the 5v and GND from Arduino.
I then put the LCD screen on the same lines for +ve and -ve and SDA & SCL pins on A4 & A5
Then I connected the push button with female-male wires, one leg to pin 3 and the other GND on breadboard.
Potentiometer is connected also with F-M wires, the middle pin to pin A3 and the others to +ve and -ve on breadboard.
After that I connected IR sensor to pin 4 & +ve and -ve on breadboard.
After I made sure the circuit is working, I mounted all the components in the cardboard.
I made holes for the push button and potentiometer.
Finally, I connected arduino cable and selected the board, the port then upload.
Cardboard parts for body
Assembling it together (bottom view)
Top view of the body
Bottle place connected with a panel for IR sensor to set on
I cut 1 cm from bottom of the last side to fit with bottle base
Buzzer connected on arduino directly
5V and GND from arduino to breadboard
LCD screen connection with breadboard and arduino
Push button connection
Potentiometer connection and I used a sellotape to fix looseness of wires
IR sensor connection on breadboard and arduino
Mounting all components in the box
Bottole place in final outcome
Final outcome of the project
Final outcome of the project in action
When I set the IR sensor it was reading LOW non stoping as if it is detecting something while actually there is not. When I move it, it blinks. So, I took it out and tested it alone to recognize its behavior. I realized that it is range is detecting the edge of the cardboard in front of it. I remembered that it is said in week tutorials that range can be adjusted. So, I searched to figure it out and I found that there is a potentiometer (as screw). I used a screw driver to configure it until it worked.
Where problem happens
IR sensor schema
In this I week I got used to make arduino do several tasks, control multiple output devices and read multiple input signals. Also, code got bigger because there is more flow and actions to perform. This made me get ready to plan for coding and final project circuit.
The WOW moment happened when we controlled the circuit in two modes manual and automatic. I felt like I am building a real projects and I am in control with the system.
We made a simple representation of a smart planting system. This system senses the moisture level in a plant's soil, as well as, weather parameters (temperature and humidity) in order to control a DC fan and a Pump using two modes manual and automatic.