Digital Scale βοΈ
Here we are going to make a Digital scale that can meausre distance using an ultrasonic sensor.
Required Hardware π β
- Arduino UNO x 1
- USB Type-B Cable x 1
- HC04-Ultrasonic sensor x 1
- 16x2 LCD Module x 1
- Jumper Wires
- A Computer
Required Software π₯οΈβ
- Arduino IDE
Step 1: Introduction to Arduino βΎοΈβ
Ardunio is a company and they build tools that help us to build electonics projects and products in a easy way. In this project, we are going to use some of arduino tools such as Arduino IDE software, Arduino UNO development board.
1.1 Arduino IDEβ
(π·src: Arduino.com)
- The Arduino Integrated Development Environment (IDE) is a crucial part of the Arduino ecosystem.
- It is a user-friendly software that allows users to write, compile, and upload code to their Arduino boards.
- The IDE is compatible with various operating systems, making it accessible to a wide range of users. With its intuitive interface and simplified syntax, even beginners find it easy to program their Arduino boards and experiment with different projects.
ToDo β β
- Download Arduino IDE : https://www.arduino.cc/en/software
- Install Arduino IDE on your computer.
1.2 Arduino Development boards also know as Dev boards or Hardware Development Kits (HDK)β
(π·src: circuitdigest.com)
- Arduino devboards serve as the foundation for numerous electronic projects. These boards come in different shapes and sizes, each catering to specific needs and capabilities.
- Some popular Arduino boards include Arduino Uno, Arduino Mega, Arduino Nano, and Arduino Due, among others.
- Each devboard features a microcontroller and various input/output pins, allowing users to interface with sensors, actuators, and other components seamlessly.
The beauty of Arduino devboards lies in their versatility.They support a wide range of sensors and actuators, such as temperature sensors, motion detectors, LCD screens, motors, and more. Whether you want to build a home automation system, a robot, a weather station, or an interactive art installation, there is an Arduino devboard suited for your project.
βοΈ Moreover, the vast Arduino community contributes to the richness of the platform. Users worldwide share their projects, tutorials, and libraries, allowing others to learn, collaborate, and build upon existing work. This spirit of open-source collaboration has played a pivotal role in making Arduino a powerful catalyst for innovation and creativity.
Here in this project, we are using the Arduino UNO model.
(π·src: Arduino.com)
ToDo β β
- Explore Arduino UNO
- Understand Arduino UNO Components
Step 2: Hello World - Blink Projectβ
Whenever we are learning new project, we will first write and run "Hello World" project. In hardware, we will do a LED "Blink" as first project.
2.1 Blink Sketchβ
In arduino, we call programs as sketch. For the first "Hello World", Copy the below code, and past to your arduino IDE.
void setup(){
pinMode(13, OUTPUT);
}
void loop(){
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}
2.2 Select the correct port and boardβ
After pasting the code from above, select the board as uno in Tools -> Board -> Arduino AVR Boards -> Arduino Uno and connect the uno with computer and select the correct port.
2.2 Upload the Sketchβ
Upload the sketch by clicking the "Upload" button. It will compile and verify your skecth and upload the program.
We can see the LED is blnking. (You don't need to attach a LED like the below image, you can see the onboard LED is blinking alone)
Step 3: Digital Scale Build π οΈβ
Now, you got an idea about how arduino works. Let's built the digital scale one by one. First, we are going to test the sensor and then display the data on the LCD display.
3.1: Test the Ultrasonic Sensorβ
In this project, to measure the distance we are using an Ultrasonic sensor.
In Ultrasonic module, transmitter part will send a ultarasonic signal and when it's hits on a object it will reflect then the reciver side will recive it. After that, we will calculate the distace from the time it required to reflect the signal.
After that, we will display the distance on a LCD display for protability.
3.2 Connection Diagramβ
Connect the HCSRO4 and Arduino as mentioned below
HCSR04 | Arduino Pin |
---|---|
GND | Arduino GND |
ECHO | Arduino D10 |
TRIG | Arduino D11 |
+5v/VCC | Arduino 5v |
Upload Ultrasonic codeβ
After the connecting, copy the code and upload. Try to read the comments in the code for better understanding.
const int trigPin = 11;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
After uploading, open the Serial monitor by cliking the right corner lense icon (1) on arduino IDE and see the distance printed on below (2).
Congratulations ππ , you are successfully intergrated the HCSR04 Ultrasonic sensor with the Arduino UNO and able to read tha data and display it on Arduino IDE Serial monitor.
3.3 Test the LCD display.β
We can't carry the computer always with our proect to see the distace right? so we need to display the distance on a display to make it more portable. For that, we are using 16x2 LCD display with I2C Interface. Before using it on a project we need to test the LCD individually and see it's working.
3.4 Connection Diagramβ
Connect the LDR and Arduino as mentioned below
LCD | Arduino Pin |
---|---|
GND | Arduino GND |
VCC | Arduino 5v |
SDA | Arduino SDA |
SCL | Arduino SCL |
Upload LCD demo codeβ
3.5 Install "LiquidCrystal_I2C" Arduino Library.β
We need to install an library in the arduino IDE before compile and upload the code. Follow the below steps and install the lib. Open Library Menue(1) , Enter the Library name ""LiquidCrystal_I2C" (2) , and Install "Frank de Brabander" library (3).
3.6 Upload Code.β
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
lcd.init(); // initialize the lcd
lcd.init();
lcd.backlight();
lcd.clear(); // clear the LCD
lcd.setCursor(2, 0); //Set cursor to character 2 on line 0
lcd.print("Hello world!");
lcd.setCursor(2, 1); //Move cursor to character 2 on line 1
lcd.print("LCD Tutorial");
}
void loop() {
}
After uploading, you can see the text printed on LCD, yeey π₯³.
Intergrate Ultrasonic Sensor + LCD Display for final project.β
Finally, we need to intergrate the both module and make the final project.
3.7 Connection Diagram.β
Connect everything as per the below diagram.
Module | Arduino Pin |
---|---|
Ultrasonic GND | Arduino GND |
Ultrasonic ECHO | Arduino D10 |
Ultrasonic TRIG | Arduino D11 |
Ultrasonic VCC | Arduino 5v |
LCD GND | Arduino GND |
LCD VCC | Arduino 5v |
LCD SDA | Arduino SDA |
LCD SCL | Arduino SCL |
3.8 Full Code.β
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int trigPin = 11;
const int echoPin = 10;
// defines variables
long duration;
int distance;
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
lcd.init(); // initialize the lcd
lcd.init();
lcd.backlight();
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
lcd.clear();
// Print a message to the LCD.
lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance "); // Prints string "Distance" on the LCD
lcd.setCursor(0, 1);
lcd.print(distance); // Prints the distance value from the sensor
lcd.print(" cm");
delay(300);
}
Conclusionβ
Congratulations ππ , you made "Digital Scale" project with Arduino , HCSR04 Ultrasonic Sensor and LCD Disply module.