*Activity Problem: Arduino Temperature Display*
*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.
3. The displayed values should be easy to understand and formatted nicely.
*Solution:*
arduino
#include <DHT.h>
// Define the type of sensor (DHT22)
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
void loop() {
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if data reading was successful
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
} else {
// Print the temperature and humidity on the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C | Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Wait a few seconds before taking the next reading
delay(2000);
*Explanation:*
- We use the DHT library to interface with the DHT22 sensor.
- 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.
- A delay is added to take readings at regular intervals.
*How to Use:*
1. Connect the DHT22 sensor to the appropriate pin (DHTPIN in this case).
2. Upload the sketch to your Arduino board.
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
Create an Arduino-based digital dice that simulates the rolling of a 6-sided
dice. When a button is pressed, the Arduino should generate a random number
between 1 and 6 and display the result on a 7-segment display.
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:
Arduino board (e.g., Arduino Uno)
A 7-segment display (common cathode or common anode)
A momentary push-button switch
Seven 220-470Ω current-limiting resistors (for the 7-segment display)
Jumper wires
Breadboard
Here are the steps to create your digital dice:
Step 1: Circuit Connections
Connect the components as follows:
Connect the common pin of the 7-segment display to ground (GND).
Connect the individual segment pins of the display to digital pins on the Arduino with
appropriate current-limiting resistors.
Connect one terminal of the momentary push-button switch to a digital pin (e.g., pin
2) and the other terminal to ground.
Connect a 10kΩ pull-up resistor from the same digital pin (e.g., pin 2) to the 5V
supply to enable internal pull-up resistor.
Power the Arduino board using USB or an external power source.
Your circuit connections should resemble this layout:
Arduino 7-Segment Display
GND Common (CA or CC)
| |
| a
| |
| 7 1
| | |
| |---|
| 6 2
| | |
| |---|
| 5
| |
| 3
| dp (if available)
| |
5V------
10kΩ
Button
GND
Step 2: Arduino Code
Here is the Arduino code to simulate the roll of a 6-sided dice:
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
#define BUTTON_PIN 2 // The pin connected to the button
#define DISPLAY_ADDR 0x70 // I2C address for the display
Adafruit_7segment display = Adafruit_7segment();
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
display.begin(DISPLAY_ADDR);
randomSeed(analogRead(0)); // Seed the random number generator
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) { // Button is pressed
int randomNumber = random(1, 7); // Generate a random number between 1 and 6
display.print(randomNumber);
display.writeDisplay();
delay(1000); // Display the result for 1 second
display.clear();
Make sure to install the Adafruit LED Backpack and Adafruit GFX libraries through the
Arduino Library Manager for this code to work.
Step 3: Upload the Code
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.