You are on page 1of 4

UG 106: Praxis I

Undergraduate Program
Handout: Arduino Tutorial Communication

September 2012 Semester


Asian Insitute of Technology
Instructors: Waqar Shahid, Matthew N. Dailey

Arduino Tutorial Communication

Introduction:
In this tutorial we will learn how to communicate with the host-machine using serialport.

Standard Serial Port


In the Arduino UNO board, pin-0 and pin-1 are used for standard TTL serial communication. The pin-0 acts
as a receive (RX) pin, while pin-1 acts as a transmit (TX) pin. These pins are connected to the corresponding
pins of the ATmega8U2 USB-to-TTL Serial chip. They are also used my the board for uploading the program
to the arduino board.
There are two other serial communication protocols i.e. SPI and TWI, which are supported by the
arduino. These two are mostly used to communicate with the sensors or devices which supports the protocol.
If we need to communicate with more then one device using standard serial protocol. or when you
need to use one serial port for debugging and another to communicate with another device, then we need a
software serial port. Fortunately, in arduino we already have such an implementation. The SoftwareSerial
Library has been developed to allow serial communication to take place on the other digital pins of Arduino.
The digital pins are used to replicate the functionality of the serial port. For todays lab we will only
play with the hardware serial port. You can play with the software serial port by download the code from
http://arduino.cc/en/Tutorial/SoftwareSerialExample
Components required: For this Lab we need the following equipment and components:
1. Arduino UNO R3
2. Proto-board
3. Digital Oscilloscope
4. Digital Multimeter
5. Jumpers (connecting wires)
6. Resistor ( 470) or ( 220)
7. DC Motor
8. L293D IC
9. push button
10. 5 Volts D.C power supply
Arduino pin numbers
Pin 2 as switch pin
pin 12 as positive terminal of motor1
pin 05 as negative terminal of motor1

pin 09 as positive terminal of motor2


pin 10 as negative terminal of motor2
pin 04 as enable1 pin for IC
pin 07 as enable2 pin for IC
pin 13 as LED pin

You can connect the circuit as shown in the Figure 1a,1b. We will use 2 x output pins from the Arduino
to control the motion of the DC motors. A switch on or off button is used to control the direction of
the motors.

(a) Schematics generated by Fritzing-0.7.7b

(b) Diagram generated by Fritzing-0.7.7b

Figure 1: Implementation Diagram


Code: Write the program to move the motor.
// Wiring/Arduino code:
// Read data from the serial and turn ON or OFF a light depending on the value
char val;
const int
const int
const int
const int
const int
const int
const int
const int

// Data received from the serial port


ledPin = 13; // Set the pin to digital I/O 4
enablePin1 = 04; // switch pin to enable motor
enablePin2 = 07; // switch pin to enable motor
motor1P1 = 12;
motor1P2 = 5;
motor2P1 = 9;
motor2P2 = 10;
switchP = 2;

void setup()
{
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
pinMode(enablePin1, OUTPUT); // pin for enable motor
pinMode(enablePin2, OUTPUT); // pin for enable motor
pinMode(motor1P1, OUTPUT); // pin for enable motor
pinMode(motor1P2, OUTPUT); // pin for enable motor
pinMode(motor2P1, OUTPUT); // pin for enable motor

pinMode(motor2P2, OUTPUT); // pin for enable motor


pinMode(switchP, INPUT); // switch the motor reverse
// no need to for define pwm or analog input
Serial.begin(9600); // Start serial communication at 9600 bps
// prints title with ending line break
Serial.println("Welcome to Arduino - Terminal");
Serial.println("Enter H to ON and any other key to OFF");
}
void loop() {

if (Serial.available()) { // If data is available to read,


val = Serial.read(); // read it and store it in val
}
if (val == 'H') { // If H was received
digitalWrite(ledPin, HIGH); // turn the LED on
digitalWrite(enablePin1, HIGH); // turn the Motor
digitalWrite(enablePin2, HIGH); // turn the Motor
if(digitalRead(switchP) == HIGH){
digitalWrite(motor1P1, HIGH);
digitalWrite(motor1P2, LOW);
digitalWrite(motor2P1, LOW);
digitalWrite(motor2P2, HIGH);
}
else{
digitalWrite(motor1P1, LOW);
digitalWrite(motor1P2, HIGH);
digitalWrite(motor2P1, HIGH);
digitalWrite(motor2P2, LOW);
}
Serial.println(" Motors are on and running");
}
else {
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
digitalWrite(enablePin1, LOW); // turn the Motor off
digitalWrite(enablePin2, LOW); // turn the Motor off
}
delay(10); // Wait 10 milliseconds for next reading
}

Conclusion and results: Press cntrl-R to compile the code. If there is no errors, then press cntrl-U to
upload the compiled program on your C Flash memory. As told earlier, once the program is uploaded
successfully, the board will automatically get a reset and after a few second will start running your code.
Press the push button to check if the motors move in the opposite directions when the signal at pin-2 is
high.

References
http://arduino.cc.
http://arduino.cc/en/Main/ArduinoBoardUno.
http://arduino.cc/en/Guide/Introduction.
www.atmel.com/Images/doc8161.pdf.
Arduino Programming Notebook by - by Brian W. Evans.
Beginning Android ADK with Arduino by Mario Bohmer
http://arduino.cc/en/Tutorial/Button
http://arduino.cc/en/Tutorial/Blink
http://arduino.cc/en/uploads/Main/arduino_Uno_Rev3-02-TH.zip.
http://arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf.
http://arduino.cc/en/Main/ArduinoBoardUno.
http://www.aishack.in/2010/07/the-three-motors-youll-ever-use-in-robotics/
http://arduino.cc/en/Tutorial/SoftwareSerialExample

You might also like