Activity Problem
Activity Problem
*Problem Statement:*
Create an Arduino sketch to read temperature data from a DHT22 (AM2302) temperature
and humidity sensor and display it on the serial monitor. Your sketch should be able to read
the temperature and humidity values and print them to the serial monitor in a user-friendly
format.
*Expected Result:*
1. The Arduino should read temperature and humidity data from the DHT22 sensor.
2. The temperature and humidity values should be displayed on the serial monitor.
*Solution:*
arduino
#include <DHT.h>
#define DHTPIN 2
void setup() {
Serial.begin(9600);
dht.begin();
void loop() {
if (isnan(temperature) || isnan(humidity)) {
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(humidity);
Serial.println("%");
delay(2000);
*Explanation:*
- In the setup() function, we start serial communication and initialize the DHT sensor.
- The loop() function reads the temperature and humidity from the sensor and checks if the
data reading was successful.
- If the reading is successful, it prints the values on the serial monitor in a user-friendly
format.
*How to Use:*
1. Connect the DHT22 sensor to the appropriate pin (DHTPIN in this case).
3. Open the Serial Monitor in the Arduino IDE (Tools > Serial Monitor).
4. You should see the temperature and humidity values displayed on the Serial Monitor,
updating every few seconds as per the delay in the loop() function.
PRELIM EXAMINATION: DICE SIMULATOR
When the button is pressed, the Arduino should simulate the roll of a 6-sided
dice. A random number between 1 and 6 should be displayed on a 7-segment display.
Creating an Arduino-based digital dice that simulates the rolling of a 6-sided dice is a fun
project. To do this, you will need the following components:
| |
| a
| |
| 7 1
| | |
| |---|
| 6 2
| | |
| |---|
| 5
| |
| 3
| dp (if available)
| |
5V------
10kΩ
Button
GND
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
display.begin(DISPLAY_ADDR);
void loop() {
display.print(randomNumber);
display.writeDisplay();
display.clear();
Make sure to install the Adafruit LED Backpack and Adafruit GFX libraries through the
Arduino Library Manager for this code to work.
Upload the code to your Arduino board using the Arduino IDE.
Step 4: Testing
Press the button on your circuit to simulate the roll of a 6-sided dice. The result will be
displayed on the 7-segment display for one second before clearing.
That's it! You've created a digital dice with an Arduino. Press the button, and it will generate
and display random numbers between 1 and 6 on the 7-segment display.