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

How to interface Serial ADC0831 with 8051 Microcontroller (AT89C51, AT89S51)

Introduction

It is an example of Serial ADC0831 To 8051 Microcontroller (AT89C51/AT89S51) Interface. ADC or Analog To Digital Converter reads analog signal level and convert digital/binary value against this level.

Circuit Diagram

Description

ADC is most widely used and important part in electronics. Most of the time in digital electronics we need to deal or process analog signals. Sensors often present their output in the form of analog signals such as temperature, light, gas and pressure sensors (analog voltage/current). As microcontroller can understand only digital quantities so there is need to convert these analog signal levels to corresponding digital value. This task of analog to digital conversion is performed by Analog To Digital Converter or ADC. Now a days in the market there are so many integrated circuits available in which whole circuit of ADC is integrated. So, ADC is available in a single chip. Some microcontrollers have built-in ADC. But, in case of traditional 8051 microcontroller we need to attach separate ADC chip. Different ADCs have different characteristics. These have different input voltage ranges. Some provide parallel output and some output data in serial stream of bits. These have different resolution say 4, 8, 10, 12 or even 24 bit. High resolution means smaller step and small step means ADC can sense small change in input signal. Let us consider an ADC with 4 bit resolution. It means it’s output may have 2(n) = 2(4) = 16 different levels. consider it’s input voltage range is 0v to 5v. So,

ADC Range = 5v – 0v = 5v

We have used 8 bit, serial output ADC ADC0831 in this project that has only three interface pins to read analog to digital converted value from it. So, less number of pins of microcontroller are used for this purpose and hardware is simple. Software is also simple in which we read ADC result by giving pulses to clock pin of ADC and reading back each bit from data pin of ADC. In parallel output ADC more pins of microcontroller are reserved and hardware becomes complex and so, difficult to troubleshoot.

Remember: Minimum parts used with minimum connections in a circuit make it simple, maintainable and easy to troubleshoot than a circuit with more parts and more connections but sometime it makes the software of microcontroller complex.

As discussed before three pins of ADC are connected to 8051 microcontroller that are

  1. Chip Select (CS)
  2. Clock (CK)
  3. Data (DO)

Remaining pins are

  1. Vref connected to 5v
  2. Vin- connected to ground
  3. Vin+ connected to input analog signal. Here, a wiper pin of variable resistor

Vin- is at 0v and Vref is at 5v so, ADC range is 5v – 0v = 5v. Analog input signal that is to be converted is applied to pin Vin+ of ADC0831. This signal may have level from 0v to 5v range. We have used variable resistor at ADC input just for testing and changing analog input. This change is sensed by microcontroller and is displayed on LCD.

Code Explanation

Here is function to initialize ADC.

1-  void Init_ADC0831()
2-  {
3-     adc_CLK = 0;
4-     adc_CS = 1;
5-     adc_Data = 1;
6-  }

In the above code we have initialized microcontroller pins for ADC, so that ADC is ready for conversion.

Following is the function that reads analog signal level through ADC.

01-  unsigned char ReadADC0831()
02-  {
03-	unsigned char Data;
04-	unsigned char i;
05-	adc_CS = 0;
06-	;;;;;;;
07-	for(i = 0; i < 10; i ++)
08-	{
09-		adc_CLK = 1;;;;;;;;
10-		Data <<= 1;
11-		Data |= adc_DATA;
12-		adc_CLK = 0;;;;;;;;
13-	}
14-	adc_CS = 1;
15-	return Data;
16-  }

At lines 03 we have defined a variable named "Data" in which we will store ADC result coming from ADC. At line 04 a counter variable for "for" loop is defined. At line 05 first we lower down or provide logic 0 to chip select pin of ADC that instruct ADC to start new conversion. After this we delay for some time at line 06 so that ADC complete it's conversion. For loop will be executed 10 times for each bit of ADC result. Actual bits are 8 but according to ADC0831 datasheet we will read 10 bits from ADC and will ignore first 2 bits. At line number 09 we set clock pin of ADC at logic high for bit reading. Line 10 space is made in variable "Data" at least significant bit position. Line 11 reads bit present on data pin of ADC and store this bit in "Data" variable at least significant bit position. After this we lower down ADC clock and cycle for single bit reading is complete. This cycle is repeated 10 times and all ADC result bits are read out of ADC. At line 14 we disable chip select by providing logic high to it and now whole ADC result is present in "Data" variable that we return for further processing.

Following is the main function or entry point of our microcontroller program

01-  void main()
02-  {
03-	unsigned char i;
04-	unsigned char Msg[20];
05-	unsigned int AverageValue;
06-	unsigned char Sample[5] = {0, 0, 0, 0, 0};

07-	InitSystem();
08-	while(1)
09-	{
10-		AverageValue = 0;
11-		for(i = 4; i > 0; i --)
12-		{
13-			Sample[i] = Sample[i - 1];
14-			AverageValue += Sample[i];
15-		}
16-		Sample[0] = ReadADC0831();
17-		AverageValue += Sample[0];
18-		AverageValue /= 5;
19-		sprintf(Msg, "   ADC = %d      \0", AverageValue);
20-		gotoxy_lcd(1, 2);
21-		print_lcd(Msg);
22-	}
23-  }

In this function we perform main task of our program. From line 03 to 06 different variable are defined that will be used next in the function. At line 07 we initialize our system by calling "InitSystem" function. "InitSystem" function initializes ADC, LCD and prints default message "ADC0831 To 8051 " on first line of 16x2 LCD. After this, while loop is executed. From line 10 to 18 we are reading ADC value and calculating 5 point moving average. From line 19 to 21 we are formatting our result in a string "Msg" and showing result on LCD. Steps from line 10 to 21 are repeated until power to microcontroller is applied.

Free Downloads

Interfacing-Serial-ADC0831-To-8051-AT89C51-1.rar with Proteus Simulation

Related Projects

8051 Basic LED Flasher

8051 To 16x2 LCD Interfaing

8051 Digital Clock On 16x2 LCD

More Projects

Categories Uncategorized

How To Convert C Code Into Assembly in Keil 8051 Compiler

You can convert C code into assembly by following these steps.

1 – Right Click on Target & select “Options for Target” like in the following diagram

Now the following dialog will appear.

2 – Now select “Listing” Tab and new page will appear like in the following figure

3 – Now tick “Assembly Code” check box and press OK button.

4 – Compile your code.

5 – Check newly generated “lst” extension file like main.LST in this example.

This file will have actual C code and Assembly code generated corresponding to this C code. In this way problem is that compiler generates routines with labels that are not self exploratory. This compiler generated assembly code is no more optimized. But it may help us in so many ways. Some times it may be useful for converting our previously written C code into assembly and save our time to code it again from beginning.

Go to Main Projects Page

Categories Uncategorized

8051 4 Digit Electronic Code Lock with LCD and Keypad

Description

It’s an example of 8051 4 Digit Electronic Code Lock with LCD and Keypad.

Circuit Diagram

Detail

This is a simple example of electronic code lock based on 8051 microcontroller. This example project also describes how can we interface 16×2 LCD and keypad. Simple Telephone keypad is used for this project. Initially when system is started “Enter Password ” is displayed on 1st row and user enters password and at each key press a ‘*’ is displayed on screen. This password is of 4 digit length. If after entering 4 digits password matches with predefined password in the program code then system activates the lock relay attached at pin # 0 of port P2 means opens lock and displays user a message that lock is opened. Now on pressing ‘*’ key user can enable lock again. If password is incorrect then system shows invalid password message and does not open the lock. Password for the lock defined in the code is “1234”. If user enters a wrong key and wants to enter password again from start then user can press ‘*’ to re-enter password in the middle.

For detail of LCD interfacing please see 8051 To 16×2 LCD Interfaing.

For basic operation & components used in an 8051 based microcontroller based systems see
8051 Basic LED Flasher.

Free Downloads

8051-4-Digit-Electronic-Code-Lock-with-LCD-and-Keypad.zip
HD44780 Datasheet

Related Projects

8051 Basic LED Flasher

8051 To 16×2 LCD Interfaing

8051 Timer0 As Second Counter On 7 Segment

8051 Digital Clock On 7 Segment (Non Multiplexed)

PC Serial RS232 Port Interface To 16×2 LCD

8051 Calculator with 16×2 LCD and Telephone Keypad

Cricket Score Board using 8051 and 7 Segment Display
More Projects

Categories Uncategorized

8051 Calculator with 16×2 LCD and Telephone Keypad

Description

It’s an example of 8051 Calculator with 16×2 LCD and Telephone Keypad.

Circuit Diagram

Detail

This example project describes how can we interface 16×2 LCD and keypad. Simple Telephone keypad is used for this project. Initially when system is started 0 is displayed on 2nd row and right most column. First user enters first operand then select an operation by pressing ‘*’ key once or again and again. When user presses ‘*’ key first multiply operation is displayed on left most upper corner. By pressing ‘*’ key again and again changes operation to be performed and this operation is displayed on upper left corner of the 16×2 LCD. Operation in this version are simply 4 arithmetic operations *,+,- and /. Maximum of 4 digit operand can be entered in this version. After selecting operation user will enter 2nd operand then press ‘#’ key that will perform selected operation and will show result on lower right corner of 16×2 LCD.

For detail of LCD interfacing please see 8051 To 16×2 LCD Interfaing.

For basic operation & components used in an 8051 based microcontroller based systems see
8051 Basic LED Flasher.

Free Downloads

8051-Calculator-with-16×2-LCD-and-Telephone-Keypad.zip
HD44780 Datasheet

Related Projects

8051 Basic LED Flasher

8051 To 16×2 LCD Interfaing

8051 Timer0 As Second Counter On 7 Segment

8051 Digital Clock On 7 Segment (Non Multiplexed)

PC Serial RS232 Port Interface To 16×2 LCD

8051 4 Digit Electronic Code Lock with LCD and Keypad

Cricket Score Board using 8051 and 7 Segment Display
More Projects

Categories Uncategorized

Cricket Score Board using 8051 and 7 Segment

Description

It’s an example of Cricket Score Board using 8051 and 7 Segment.

Circuit Diagram

Detail

In this example we have interfaced 7 segment and touch buttons. Initially score of 0 is displayed on seven segments. When user presses button B0 4 is added in the score. When B1 is pressed a score of 6 is added. On pressing B2 1 is decremented from score. And when B3 is pressed 1 is incremented in the score. This score is also updated on seven segment when it is changed. For detail of how can we interface 7 segment displays please see 8051 To 7 Segment Display Interfacing.

For basic operation & components used in an 8051 based microcontroller based systems see
8051 Basic LED Flasher.

Free Downloads

Cricket-Score-Board-using-8051-and-7-Segment.zip

Related Projects

8051 Basic LED Flasher

8051 To 7 Segment Display Interfacing

8051 Timer0 As Second Counter On 7 Segment

8051 Digital Clock On 7 Segment (Non Multiplexed)

8051 Digital Clock On 16×2 LCD

PC Serial RS232 Port Interface To 7 Segment

More Projects

Categories Uncategorized