Interface Digital and Analog I/O Devices (Arduino Interfacing)
Interface Digital and Analog I/O Devices (Arduino Interfacing)
Hiren D. Shukla
Lecturer-EC
Objectives
digitalWrite(r2, HIGH);
digitalWrite(g1, HIGH);
delay(5000); // wait 5 seconds
digitalWrite(y1, HIGH);
digitalWrite(y2, HIGH);
digitalWrite(r1, LOW);
digitalWrite(r2, LOW);
digitalWrite(g1, LOW);
digitalWrite(g2, LOW);
delay(5000);
}
Interface a Switch
Code
Int ledPin=13;
Int inpin=2;
Void setup()
{
pinMode(ledpin,OUTPUT);
pinMode(inpin,INPUT);
}
Void loop()
{
if (digitalRead(inpin) == HIGH)
{
digitalWrite(ledpin, HIGH);
delay(1000);
digitalWrite(ledpin,LOW);
delay(1000);
}
}
Write a program to switch the mode of
LED.
Write a Program to switch the mode of LED.
int in = 2; // the number of the input pin
int out = 13; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin // the follow
variables are long's because the time, measured in miliseconds, // will
quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(in, INPUT);
pinMode(out, OUTPUT);
}
Continue…
void loop()
{
reading = digitalRead(in); // if the input just went from LOW and HIGH and we've waited
long enough // to ignore any noise on the circuit, toggle the output pin and remember //
the time
if (reading == HIGH && previous == LOW && millis() - time > debounce)
// interrupt will increment a variable after every millisecond
{
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(out, state);
previous = reading;
}
Interface 7 Segment LED
Serial Communication
Sets the data rate in bits per second (baud) for serial data
transmission. For communicating with the computer, use
one of these rates: 300, 600, 1200, 2400, 4800, 9600,
14400, 19200, 28800, 38400, 57600, or 115200.
Syntax
Serial.begin(speed)
Interface LM35 (Temperature Sensor)S
LM 35 Discription
LM35 Code
Float temp;
int temp_pin=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
temp=analogRead(temp_pin);
temp=temp * 0.48828;
Serial.print (“ Temperature = “);
Serial.print(temp);
Serial.print(“ *C”);
Serial.println();
delay(1000);
}
Accelerometer (ADXL 3C5C) Arduino
Interfacing.
int groundpin = 18; // analog input pin 4 -- ground
int powerpin = 19; // analog input pin 5 -- voltage
int xpin = A3; // x-axis of the accelerometer
int ypin = A2; // y-axis
int zpin = A1; // z-axis (only on 3-axis models)
void setup()
{
Serial.begin(9600);
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}
void loop()
{
Serial.print(analogRead(xpin));
Serial.print("\t");
Serial.print(analogRead(ypin));
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
delay(100);
}
Keypad Interfacing
Code
#include <Keypad.h>
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
}
void loop(){
char key = keypad.getKey();
if (key != NO_KEY){
Serial.println(key);
}
}
Interfacing DC motor
Code
int motor_forward = 7;
int motor_reverse = 6;
void setup() {pinMode(motor_forward, OUTPUT);
pinMode(motor_reverse, OUTPUT); }
void loop() { digitalWrite(motor_forward,1);
digitalWrite(motor_reverse,0);
delay(5000);
digitalWrite(motor_forward,0);
digitalWrite(motor_reverse,1);
delay(5000);
digitalWrite(motor_forward,0);
digitalWrite(motor_reverse,0);
delay(5000);
Interfacing 16x2 LCD
Code
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
Switch Case
void setup() {
Serial.begin(9600);
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT); }
void loop() {
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case 'a':
digitalWrite(2, HIGH);
break;
case 'b':
digitalWrite(3, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
case 'd':
digitalWrite(5, HIGH);
break;
case 'e':
digitalWrite(6, HIGH);
break;
default:
for (int thisPin = 2; thisPin < 7; thisPin++) {
digitalWrite(thisPin, LOW);
} } }
Programs