Arduino Interfacing Codes
Arduino Interfacing Codes
LCD INTERFACING
• /*
• LiquidCrystal Library - Hello World
• The circuit:
• * LCD RS pin to digital pin 12
• * LCD Enable pin to digital pin 11
• * LCD D4 pin to digital pin 5
• * LCD D5 pin to digitalpin 4
• * LCD D6 pin to digital pin 3
• * LCD D7 pin to digital pin 2
• * LCD R/W pin to ground
• * LCD VSS pin to ground
• * LCD VCC pin to 5V
• * 10K resistor:
• * ends to +5V and ground
• * wiper to LCD VO pin (pin 3)
• */
•
• // include the library code:
• #include <LiquidCrystal.h>
•
• // initialize the library by associating any needed LCD interface pin
• // with the arduino pin number it is connected to
• const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
• LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
•
• void setup() {
• // set up the LCD's number of columns and rows:
• lcd.begin(16, 2);
• // Print a message on both lines of the LCD.
• lcd.setCursor(2,0); //Set cursor to character 2 on line 0
• lcd.print("Hello World!");
•
• lcd.setCursor(4,1); //Move cursor to character 2 on line 1
• lcd.print("LCD_I2C");
• }
•
• void loop() {
• }
LCD INTERFACING USING I2C
• #include <LiquidCrystal_I2C.h>
•
• LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
•
• void setup() {
• lcd.init();
• lcd.clear();
• //lcd.backlight(); // Make sure backlight is on3
•
• // Print a message on both lines of the LCD.
• lcd.setCursor(3,0); //Set cursor to character 2 on line 0
• lcd.print("Hello World!");
•
• lcd.setCursor(5,1); //Move cursor to character 2 on line 1
• lcd.print("LCD_I2C");
• }
•
• void loop() {
• }
KEYPAD INTERFACING
KEYPAD INTERFACING (SWITCH CASE)
• #include
• const byte ROWS = 4; // Four rows
• const byte COLS = 3; // Three columns
• // Define the Keymap
• char keys[ROWS][COLS] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'#','0','*'} };
• // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
• byte rowPins[ROWS] = { 9, 8, 7, 6 };
• // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
• byte colPins[COLS] = { 12, 11, 10 };
• // Create the Keypad Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
• #define ledpin 13
• void setup()
• {
• pinMode(ledpin,OUTPUT);
• digitalWrite(ledpin, HIGH);
• Serial.begin(9600);
• }
• void loop()
• {
• char key = kpd.getKey();
• if(key) // Check for a valid key.
• {
• switch (key)
• {
• case '1':
• Serial.println(key);
• break;
• case '2':
• Serial.println(key);
• break;
• case '3':
• Serial.println(key);
• break;
• case '4':
• Serial.println(key);
• break;
• case '5':
• Serial.println(key);
• break;
• case '6':
• Serial.println(key);
• break;
• case '7':
• Serial.println(key);
• break;
• case '8':
• Serial.println(key);
• break;
• case '9':
• Serial.println(key);
• break;
• case '0':
• Serial.println(key);
• break;
• case '*':
• Serial.println(key);
• break;
• case '#':
• Serial.println(key);
• break;
• }
• } https://www.instructables.com/interface-keypad-with-arduino/
• }
DC motor INTERFACING
• A DC motor (Direct Current motor) is the most
common type of motor. DC motors normally
have just two leads, one positive and one
negative. If you connect these two leads
directly to a battery, the motor will rotate. If
you switch the leads, the motor will rotate in
the opposite direction.
•
https://www.tutorialspoint.com/arduino/arduino_dc_motor.htm
• Do not drive the motor directly from Arduino board pins.
This may damage the board. Use a driver Circuit or an IC.
We will divide this chapter into three parts −
• Just make your motor spin
• Control motor speed
• Control the direction of the spin of DC motor
• Components Required
• 1x Arduino UNO board
• 1x PN2222 Transistor
• 1x Small 6V DC Motor
• 1x 1N4001 diode
• 1x 270 Ω Resistor
circuit diagram
Precautions
Take the following precautions while making the
connections.
First, make sure that the transistor is connected in the
right way. The flat side of the transistor should face the
Arduino board as shown in the arrangement.
Second, the striped end of the diode should be
towards the +5V power line according to the
arrangement shown in the image.
Spin ControlArduino Code
• int motorPin = 3;
• void setup()
• {}
• void loop()
• { digitalWrite(motorPin, HIGH);
• }
• The transistor acts like a switch, controlling the power to
the motor. Arduino pin 3 is used to turn the transistor on
and off and is given the name 'motorPin' in the sketch.
• Result
• Motor will spin in full speed when the Arduino pin number
3 goes high.
Motor Speed Control
• Code
• The transistor acts like a switch, controlling the power of the motor.
Arduino pin 3 is used to turn the transistor on and off and is given
the name 'motorPin' in the sketch.
• When the program starts, it prompts you to give the values to
control the speed of the motor. You need to enter a value between
0 and 255 in the Serial Monitor.
• In the 'loop' function, the command 'Serial.parseInt' is used to read
the number entered as text in the Serial Monitor and convert it into
an 'int'. You can type any number here. The 'if' statement in the
next line simply does an analog write with this number, if the
number is between 0 and 255.
• Result
• The DC motor will spin with different speeds according to the value
(0 to 250) received via the serial port.
Motor Speed Control
• int motorPin = 9;
• void setup() { pinMode(motorPin, OUTPUT);
Serial.begin(9600);
• while (! Serial);
• Serial.println("Speed 0 to 255");
• } void loop() { if (Serial.available()) { int speed =
Serial.parseInt();
• if (speed >= 0 && speed <= 255) {
analogWrite(motorPin, speed);
• }}}