Chapter_3_Basic_Projects
Chapter_3_Basic_Projects
Description
How-To Guide
1. Gather Components:
Steps
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an
output
pinMode(buttonPin, INPUT); // Set the button pin as an
input
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the state
of the button
The code initializes two variables buttonPin and ledPin to store the pin
numbers connected to the pushbutton and LED, respectively.
In the setup() function, it sets the pin modes: ledPin as an output and
buttonPin as an input.
The loop() function continuously checks the state of the button using:
digitalRead(buttonPin).
If the button state is HIGH (pressed), it turns on the LED by setting ledPin
to HIGH.
If the button state is LOW (released), it turns off the LED by setting ledPin
to LOW.
Description
This project aims to read data from a temperature and humidity sensor using
an Arduino Uno R4. The DHT11 (temperature & humidity) sensor is commonly
used for this purpose due to its simplicity and accuracy. The sensor provides
digital output, making it easy to interface with the Arduino. By reading
temperature and humidity data, you can monitor environmental conditions in
various applications, such as home automation, weather stations, and
greenhouse monitoring systems.
How-To Guide
1. Gather Components:
1 x Arduino Uno R4
1 x Breadboard
1 x DHT11 Temperature and Humidity Sensor
3 x Jumper wires
sensor
3.3V/5V VCC
GND GND
D2 DATA
Ensure the connections are secure and there are no loose wires.
3. Install Library:
void setup() {
// initialize the sensor
Serial.begin(9600);
dht11.begin();
}
void loop() {
// wait a few seconds between measurements.
delay(1000);
// read humidity
float humi = dht11.readHumidity();
// read temperature as Celsius
float tempC = dht11.readTemperature();
// read temperature as Fahrenheit
float tempF = dht11.readTemperature(true);
// check if any reads failed
if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
Serial.println("Failed to read from DHT11 sensor!");
} else {
Serial.print(" Humidity: ");
Serial.print(humi);
Serial.print("%");
Serial.print(" ");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print("°C");
Serial.print(" ");
Serial.print("Fahrenheit: ");
Serial.print(tempF);
Serial.println("°F");
}
}
Open the Serial Monitor in the Arduino IDE (Tools -> Serial Monitor).
You should see temperature and humidity readings printed every few seconds.
Verify that the readings change when you blow on the sensor or change the
surrounding environment.
Description
This project focuses on reading data from a raindrop sensor using an Arduino
Uno R4 minima. The raindrop sensor detects the presence of water or
raindrops on its surface and provides an analog output signal that varies
based on the amount of water detected. By interfacing the raindrop sensor
with an Arduino, you can monitor rainfall or detect the presence of water in
various applications, such as weather stations, automated irrigation systems,
and leak detection systems.
How-To Guide
1. Gather Components:
minima
5V VCC
GND GND
D7 DO
NC AO
minima
D8 +
GND GND
3. Write the Arduino Code:
Here's a basic example code to read data from the raindrop sensor and print it
to the serial monitor:
const int raindropPin = A0; // Analog pin connected to the
raindrop sensor
void setup() {
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int raindropValue = analogRead(raindropPin); // Read
analog value from the sensor
Serial.print("Raindrop Value: ");
Serial.println(raindropValue); // Print the value to the
serial monitor
delay(1000); // Delay for stability
}
The code initializes a variable raindropPin to store the analog pin connected
to the raindrop sensor.
In the setup() function, it initializes serial communication at a baud rate of
9600.
The loop() function continuously reads analog values from the raindrop
sensor using analogRead(raindropPin).
It prints the analog values to the serial monitor, allowing you to monitor the
sensor readings in real-time.
The delay(1000) function adds a 1-second delay between readings for
stability.
Open the Serial Monitor in the Arduino IDE (Tools -> Serial Monitor).
You should see the analog values from the raindrop sensor being printed to the
serial monitor.
Observe how the values change when water is present or absent on the sensor
surface.
If no rain drop on the sensor pad, it will shows:
Through experiments, it can be found that when raindrops fall, the value of the
analog quantity will drop from 1023 to about 500, which means that we can
judge the amount of rainfall through the number of the analog quantity and the
amount of water!
Of course, if you only want the amount of one state, you can directly connect
the digital pin to the DO pin, which will only provide high and low level
messages, such as 1 for high level and 0 for low level.
void setup() {
Serial.begin(9600);
pinMode(SIGNAL_PIN, INPUT); // configure D7 pin as an
OUTPUT
pinMode(buzzer, OUTPUT);
}
void loop() {
value = digitalRead(SIGNAL_PIN); // read the analog
value from sensor
Serial.print("Digital Output: "); // Print output to
the serial port
Serial.println(value);
When a raindrop falls on the sensor, the buzzer goes off. Then open the serial
monitor you can see this.
This project provides practical experience in sensor interfacing and data
acquisition with Arduino. By reading data from the raindrop sensor, you can
gain insights into environmental conditions and develop solutions for various
applications. Have fun experimenting with your raindrop sensor project!
Description
How-To Guide
1. Gather Components:
Here's a basic example code to initialize the display and draw a simple shape
(e.g., a rectangle) on the screen, before you doing that, you need to adding
library from a zip file which coming with package’s USB dongle, it stored in
libraries folder in the USB dongle.
Then you can copy the code to the Arduino IDE and you need to add the
library to the project, open the flash drive you buy in this kit, you will see the
library. Then find the .zip file we can add it as a library.
#include <Arduino_GFX_Library.h>
/
***********************************************************
********************
* End of Arduino_GFX setting
**********************************************************
********************/
void setup(void)
{
Serial.begin(9600);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
// Serial.setDebugOutput(true);
// while(!Serial);
Serial.println("Arduino_GFX Hello World example");
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
// Init Display
if (!gfx->begin())
{
Serial.println("gfx->begin() failed!");
}
gfx->fillScreen(BLACK);
gfx->setCursor(10, 10);
gfx->setTextColor(PURPLE);
gfx->setTextSize(3, 3);
gfx->println("Hello World!");
gfx->setCursor(10, 50);
gfx->setTextColor(NAVY);
gfx->setTextSize(3, 3);
gfx->println("5");
gfx->setCursor(30, 50);
gfx->setTextColor(DARKGREEN);
gfx->setTextSize(3, 3);
gfx->println("2");
gfx->setCursor(50, 50);
gfx->setTextColor(DARKCYAN);
gfx->setTextSize(3, 3);
gfx->println("P");
gfx->setCursor(70, 50);
gfx->setTextColor(MAROON);
gfx->setTextSize(3, 3);
gfx->println("i");
// Draw a circle
gfx->drawEllipse(120,70,20,20,MAGENTA);
}
void loop()
{
delay(1000); // 1 second
}
Once the code is uploaded successfully, you should see a red rectangle being
drawn on the display, followed by a black screen, with a delay of 2 seconds
between each action.
If the display shows unexpected behavior or doesn't respond, double-check the
connections and code.
7. Code Explanation:
bus: It defines the communication bus used by the display. In this case, it's
initialized as a Hardware SPI bus, specifying the pins for Data/Command
(DC) and Chip Select (CS).
gfx: It initializes the graphics object using the ST7796 driver, specifying the
communication bus (bus), the pin for Reset (RESET), the address (0),
and whether the display is vertically flipped (true).
void setup(void)
{
Serial.begin(9600);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
In the setup function:
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
A message is printed to the serial monitor indicating the start of the
program.
Pin 7 is set HIGH, possibly turning on the external device (e.g., backlight).
// Init Display
if (!gfx->begin())
{
Serial.println("gfx->begin() failed!");
}
gfx->fillScreen(BLACK);
The display is initialized. If initialization fails, an error message is printed
to the serial monitor.
The display is filled with black color using the fillScreen() function.
gfx->setCursor(10, 10);
gfx->setTextColor(PURPLE);
gfx->setTextSize(3, 3);
gfx->println("Hello World!");
Text "Hello World!" is printed on the screen with purple color, at
coordinates (10, 10), and with text size 3.
This project provides a starting point for interfacing the 1.3-inch IPS 240x240
pixels RGB display with an Arduino Uno R4. You can further explore the
capabilities of the display by adding text, images, animations, and interactive
elements. Enjoy experimenting with your display project!