Random Number Generator With LCD Display
by AahanP in Circuits > Arduino
174 Views, 0 Favorites, 0 Comments
Random Number Generator With LCD Display
This Arduino-based circuit generates and displays a random number between 1 and 6 upon the press of a button. When the button is pressed, the Arduino generates a random number within this range, which is then displayed using LEDs; each LED corresponds to a specific number between 1 and 6, lighting up to represent the generated number. Simultaneously, the generated number is displayed in its decimal form on an LCD screen, allowing for easy reading and verification. The user interacts with the circuit through the button, triggering the Arduino to generate a new random number and update both the LED display and the LCD screen accordingly. This project illustrates the principles of random number generation, user interaction, and interfacing with LEDs and LCDs using an Arduino.
Supplies
Wire the Dice
555 Timer IC Connections:
Pin 1: Connected to the ground (GND).
Pin 2: Connected to the junction of resistor R1 (1K ohms) and capacitor C1 (10uF).
Pin 3: Connected to pin 14 of the CD4017B decade counter. This is the output pin which sends clock pulses to the CD4017B.
Pin 4: No connection.
Pin 5: No connection.
Pin 6: Connected to pin 2.
Pin 7: No connection.
Pin 8: Connected to the positive supply voltage and push button.
CD4017B Decade Counter IC Connections:
Pin 1: Connected to LED5.
Pin 2: Connected to LED1.
Pin 3: Connected to LED4.
Pin 4: Connected to LED2.
Pin 5: No connection.
Pin 6: No connection.
Pin 7: Connected to LED3.
Pin 8: Connected to the ground (GND).
Pin 9: No connection.
Pin 10: Connected to LED6.
Pin 11: No connection.
Pin 12: No connection.
Pin 13: Connected to the ground. This pin is used to inhibit the counter, but grounding it allows normal operation.
Pin 14: Connected to pin 3 of the 555 timer. This is the clock pulse input pin which advances the counter.
Pin 15: Connected to pin 5.
Pin 16: Connected to the positive supply voltage.
Wire the LCD Display
Add wiring to all the ports of the LCD Display, later will show where to connect to Arduino.
Connect Everything to Arduino
LCD Display Connections
SCL pin: Connects to analog pin A5.
SDA pin: Connects to analog pin A4.
VCC pin: Connects to the positive side of bread board
GND pin: Connects to the negative side of bread board
Dice Connections
LED1: Connects to digital pin 8.
LED2: Connects to digital pin 9.
LED3: Connects to digital pin 10.
LED4: Connects to digital pin 11.
LED5: Connects to digital pin 12.
LED6: Connects to digital pin 13.
Reset Button: Connects to digital pin 2.
VCC and Ground Connections
VCC: Connects to the positive side of bread board, connecting to the 5V pin in Arduino.
Ground: Connects to the negative side of bread board, connecting to the GND pin in Arduino.
Code the Arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the I2C LCD with the I2C address (0x27 is a common address)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define individual pins for the LEDs connected to the decade counter
const int ledPin1 = 8;
const int ledPin2 = 9;
const int ledPin3 = 10;
const int ledPin4 = 11;
const int ledPin5 = 12;
const int ledPin6 = 13;
// Define the button pin
const int buttonPin = 2;
void setup() {
// Initialize the I2C LCD
lcd.init();
lcd.backlight();
// Set all the LED pins as inputs
pinMode(ledPin1, INPUT);
pinMode(ledPin2, INPUT);
pinMode(ledPin3, INPUT);
pinMode(ledPin4, INPUT);
pinMode(ledPin5, INPUT);
pinMode(ledPin6, INPUT);
// Set the button pin as input with pullup resistor
pinMode(buttonPin, INPUT_PULLUP);
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print("Dice Value:");
}
void loop() {
static int lastDiceValue = 0;
int currentDiceValue;
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
// While the button is held down, update the LCD with the current dice value
while (digitalRead(buttonPin) == LOW) {
currentDiceValue = readDiceValue();
if (currentDiceValue != lastDiceValue) {
lastDiceValue = currentDiceValue;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 1);
lcd.print(currentDiceValue);
}
delay(100); // Small delay to debounce and update the display
}
// When button is released, display the final dice value
currentDiceValue = readDiceValue();
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 1);
lcd.print(currentDiceValue);
}
}
int readDiceValue() {
if (digitalRead(ledPin1) == HIGH) return 1;
if (digitalRead(ledPin2) == HIGH) return 2;
if (digitalRead(ledPin3) == HIGH) return 3;
if (digitalRead(ledPin4) == HIGH) return 4;
if (digitalRead(ledPin5) == HIGH) return 5;
if (digitalRead(ledPin6) == HIGH) return 6;
return 0;
}