Electronic Dice Using Arduino

Description

It is Electronic Dice Game Using Arduino. It is basic arduino learning project that shows how we access arduino pins for writing. Like in basic arduino LED flasher

Circuit Diagram

In circuit diagram 7 LEDs are connected each with 220 Ohm resistor in series to arduino.

Detail

It is a game in which we press button connected to pin 6 of arduino. When button is pressed different patterns of dice game are produces in fast speed. And when we release this button a random pattern is selected and shown on LEDs. This is simple example in which we generate random number from 1 to 6 range and display this number on LEDs.

Software

Arduino program is simple but lengthy. It first set pin mode for button/key as input with internal pullup resistor enabled and for LEDs as outputs. Initially displays 1 on LEDs. Calls srand() function for random number generation. This function is called once. Then in loop() function when key is pressed it generates random numbers from 1 to 6 range and display on LEDs. When key is released random number generation is stopped and final number is displayed on LEDs. Here is program


#include <stdlib.h>

// Define macro or formulae for random number
// generation from 1 to 6.
#define GetRandomNumber() ((rand() % 6) + 1)

void Display(char Value);
void turnOffAllLeds();

const int Switch = 6;

const int Led1 = 7;
const int Led2 = 8;
const int Led3 = 9;
const int Led4 = 10;
const int Led5 = 11;
const int Led6 = 12;
const int Led7 = 13;

void setup() {
  // put your setup code here, to run once:
  pinMode(Switch, INPUT_PULLUP);
  pinMode(Led1, OUTPUT);
  pinMode(Led2, OUTPUT);
  pinMode(Led3, OUTPUT);
  pinMode(Led4, OUTPUT);
  pinMode(Led5, OUTPUT);
  pinMode(Led6, OUTPUT);
  pinMode(Led7, OUTPUT);

  // Turn off all Leds.
  //turnOffAllLeds();
  Display(1);
  srand(50);
}

void loop() {
  char randomNumber;
  // put your main code here, to run repeatedly:
  while(digitalRead(Switch));
  while(!digitalRead(Switch))
  {
    // Generate random number.
    randomNumber = GetRandomNumber();

    // Display this random number
    // on Leds.
    Display(randomNumber);
    // Give some delay.
    delay(10);
  }
    // Generate random number last time.
    randomNumber = GetRandomNumber();
    // Finally display this number on LEDs.
    Display(randomNumber);
}

void turnOffAllLeds()
{
  digitalWrite(Led1, HIGH);
  digitalWrite(Led2, HIGH);
  digitalWrite(Led3, HIGH);
  digitalWrite(Led4, HIGH);
  digitalWrite(Led5, HIGH);
  digitalWrite(Led6, HIGH);
  digitalWrite(Led7, HIGH);
}

// This function displays an integer value from
// 0 to 6 on LEDs.
void Display(char Value)
{
  // Switch off all LEDs.
  //Led1 = Led2 = Led3 = Led4 = Led5 = Led6 = Led7 = 1;
  turnOffAllLeds();
  switch(Value)
  {
  case 1:
    digitalWrite(Led4, LOW);
    break;
  case 2:
    digitalWrite(Led1, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 3:
    digitalWrite(Led1, LOW);
    digitalWrite(Led4, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 4:
    digitalWrite(Led1, LOW);
    digitalWrite(Led3, LOW);
    digitalWrite(Led5, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 5:
    digitalWrite(Led1, LOW);
    digitalWrite(Led3, LOW);
    digitalWrite(Led4, LOW);
    digitalWrite(Led5, LOW);
    digitalWrite(Led7, LOW);
    break;
  case 6:
    digitalWrite(Led1, LOW);
    digitalWrite(Led2, LOW);
    digitalWrite(Led3, LOW);
    digitalWrite(Led5, LOW);
    digitalWrite(Led6, LOW);
    digitalWrite(Led7, LOW);
    break;
  }
}


Categories Uncategorized

Thermometer Using LM35D Arduino And LCD

Description

It is a LCD Thermometer Using Arduino and LM35D temperature sensor. It shows temperature on 16×2 LCD in degree centigrade.

Circuit Diagram

In circuit diagram 16×2 LCD is connected to arduino. LM35D is connected to analog channel input of arduino.

Detail

LCD is connected in conventional 4 bit mode and LM35D temperature sensor is connected to first analog input channel of arduino. LM35D gives analog output corresponding to temperature. When temperature of environment changes it’s output voltages also changes. It gives 10mv per degree centigrade, means if output of LM35D is 10mv then surrounding temperature will be 1 degree centigrade. If output changes to 100mv then surrounding temperature will be 10 degree centigrade. Our arduino program reads this voltage level and converts it into corresponding temperature value. And then displays this value on 16×2 LCD in centigrade.

Software

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

// Define analog pin where
// LM35D is connected.
const int LM35DPin = A0;

void setup() {
  // put your setup code here, to run once:

  // Setup LCD
  lcd.begin(16, 2);

  lcd.print("Temperature");

}

void loop() {
  // put your main code here, to run repeatedly:
  float temperature = analogRead(LM35DPin);
  temperature = temperature * 5000;
  temperature /= 10240;
  lcd.setCursor(0,1);
  lcd.print(temperature);
  lcd.print((char) 223);
  lcd.print('C');
  delay(300);
}

Categories Uncategorized

PC Serial RS232 Interface With Arduino

Description

It is serial RS232 Port interface example with Arduino. Serial Port is a channel for communication with microcontroller/arduino.

Circuit Diagram

Circuit diagram is simple and there is no need of extra component. All hardware interface between arduino microcontroller and PC is already provided in arduino board. Here is simple diagram in proteus to use with serial terminal in proteus.

Detail

Serial port is used to communicate with other devices like microcontrollers and PCs. It is most commonly used method for communication between arduino and computer. In serial RS232 interface all data bits are transmitted and received one bit at a time in a stream. So there is series of bits on single wire/pin. In arduino serial port is implemented over USB. There is USB to serial converter device required between arduino microcontroller and PC that is already implemented in arduino board. So, there is no need to add it additionally. This project is the base for so many other projects.

Software


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.write("Started...");

}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available())
  {
    Serial.write(Serial.read() + 2);
  }

}

In software setup() function we first initialize serial channel at 9600 bits/sec. Then we send “Started…” string towards PC. You can see how this “Started…” string is received in terminal in proteus shown in above diagram. You will also see next in video that how this string “Started…” is received in PC. In loop() function we check that if any received character is available? then read this character add 2 to it and send back towards PC.
Here is video for further clarification.

20170601_161518 from Rashid Mehmood on Vimeo.

Categories Uncategorized

16×2 LCD Interface With Arduino

Today I am going to tell you how to interface 16×2 LCD with Arduino.

Circuit Diagram

LCD

Display has very importance in embedded systems. It is used to interact humans with system. Instructions and device function results are shown on displays. Without display we cannot see what operation is being performed inside the arduino/microcontroller. LCDs are most widely used in embedded systems to provide user interface. In this example I am using HD44780 based 16×2 LCD. This LCD can be interfaced using 8 bit or 4 bit mode. In 8 bit mode all 8 data pins are used while in 4 bit mode only 4 pins of LCD are used. In this project I have used 4 bit mode of operation. 16×2 means it has 16 columns and 2 rows. HD44780 controller is installed on other sizes of LCDs like 16×1, 16×4, 20×2, 20×4 etc.

LCD.jpg
Click to Enlarge

Pin-Out

Pin Symbol Function
1 Vss ground (0 V)
2 Vdd power (4.5 – 5.5 V)
3 Vo contrast adjustment
4 RS H/L register select signal
5 R/W H/L read/write signal
6 E H/L enable signal
7-14 DB0 – DB7 H/L data bus for 4- or 8-bit mode
15 A (LED+) backlight anode
16 K (LED-) backlight cathode

We can read & write data to LCD but to keep things simple we have hardwired R/W line to ground for only writing. It means we can only print on LCD but cannot read back content written in LCD RAM.

Software

In arduino we use builtin library for LCD interfacing “LiquidCrystal.h”. And this makes LCD interfacing with arduino simple and easy.

#include <LiquidCrystal.h>

// Define object of LiquidCrystal class.
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);

  // Print welcome message
  // to LCD.
  lcd.print("Welcome!");
  
  // Set cursor to next line and 
  // first column.
  lcd.setCursor(0,1);
  
  lcd.print("micro-digital.net");

}

void loop() {
  // put your main code here, to run repeatedly:

}
Categories Uncategorized

Led Flasher With Arduino

It is simple and basic arduino project. You can say it welcome/hello world project in arduino programming. In this project I will first blink on board LED of arduino and then will use off board LED.

Blinking On Board Arduino LED

Arduino is a prototyping board for embedded systems development. In this example I am using Arduino UNO board. In this board ATMega328 microcontroller is installed. On board arduino LED is connected to pin number 13. Here is the circuit with off board LED.

What Will You Need

For on board LED you need only

  1. 1 x Arduino Board
  2. 1 x USB Cable
  3. 1 x Computer with Arduino IDE installed

For off board LED you need some more parts

  1. 1 x Bread Board
  2. 1 x LED
  3. 1 x 150 Ω Resistor
  4. 3 x Jumper Wires

Software


// Define pin 13 as LED pin
const int LED = 13;

void setup() {
  // put your setup code here, to run once:

  // Set LED pin as output.
  pinMode(LED, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  // Set LED pin at high/5V level.
  digitalWrite(LED, HIGH);
  // Delay for 500 ms.
  delay(500);
  // Set LED pin at low/0V level.
  digitalWrite(LED, LOW);
  // Delay for 500 ms.
  delay(500);
}
Categories Uncategorized