Arduino Part 1 Topics Microcontrollers Programming Basics Digital

Arduino Part 1 Topics: Microcontrollers Programming Basics Digital Output Serial Communication

Workshop Overview • • Introduction to microcontroller and Arduino The Hardware: Arduino UNO The Software: The Arduino IDE The Language : Arduino-C language Hands-on Project 1: Talking to arduino Hands-on Project 2: LED blinking Hands-on Project 3: Serial Controlled LED

What is an Arduino? • An open-source platform used for building electronics projects • A complete controller with CPU, RAM, Flash memory, and input/output pins, all on a single chip

The Hardware : Arduino UNO 1. USB(Power) 2. Barrel jack(Power) 3. Ground 4. 5 V supply 5. 3. 3 V supply 6. Analog input 7. Digital input 8. PWM output 9. Analog Ref input 10. Reset Button 11. Power indicator 12. RX, TX indicator 13. Main IC 14. Voltage Regulator 15. Built in test LED connected to pin 13

Getting Started • Check out: http: //arduino. cc/en/Guide/Home. Page 1. Download & install the Arduino environment (IDE) 2. Connect the board to your computer via the USB cable 3. If needed, install the drivers 4. Launch the Arduino IDE 5. Select your board 6. Select your serial port

The Software : Arduino IDE 1 2 3 4 5 } 6 1. 2. 3. 4. 5. 6. 7. 8. }7 }8 Verify button Upload button New file button Open file button Safe file button Program area Status bar Notification area

Select Serial Port and Board

Opening sample program

Our First program/sketch What’s Next? ? • Press verify button to check for errors • Press upload button to load the program into the arduino • Observe the on-board LED

Status Messages todbot. com/blog/bionicarduino

The Language : Arduino - C Basic program structure : setup( ): A function present in every Arduino. Run once before the loop( ) function. Often used to set pinmode to input or output. loop( ): A function present in every single Arduino sketch. This code happens over and over again. The loop( ) is where (almost) everything happens. The one exception to this is setup( ) and variable declaration.

The Language : Arduino - C The setup: • To assign a port to read signal use: pin. Mode(pin. Number, INPUT); • To assign a port to send signal use: pin. Mode(pin. Number, OUTPUT) • To initialize serial communication at 9600 bits per second : Serial. begin(9600);

The Language : Arduino - C Digital I/O: • Digital Input/Output uses the Digital pins, but Analog In pins can be used as Digital • To receive a Digital signal use: digital. Read(pin. Number); • To send a Digital signal use: digital. Write(pin. Number, value); • Digital Input and Output are always either HIGH or LOW Analog I/O: • Analog Input uses the Analog In pins, Analog Output uses the PWM pins • To receive an Analog signal use: analog. Read(pin. Number); • To send a PWM signal use: analog. Write(pin. Number, value); • Analog Input values range from 0 to 1023 (1024 values because it uses 10 bits, 210) • PWM Output values range from 0 to 255 (256 values because it uses 8 bits, 28).

The Language : Arduino - C Time/delay functions: • delay(ms) : eg. To wait for 1 sec, use delay(1000) • delay. Microseconds(us) : eg. To wait for 0. 001 sec, use delay. Microseconds(1000) • millis() : once this function is called, it will update the variable every miliseconds • micros() : once this function is called, it will update the variable every microseconds • More commands: arduino. cc/en/Reference/Home. Page

The Language : Arduino - C Serial communication functions: Used for communication between the Arduino board and a computer or other devices. • Serial. begin(9600) : to opens serial port, sets data rate to 9600 bps • Serial. print() : Prints data to the serial port as human-readable ASCII text. • Serial. println() : Prints data at the following line • Serial. available() : Get the number of bytes (characters) available for reading from the serial port. • Serial. read() : read the incoming data byte • Serial. write() : Writes binary data to the serial port. This data is sent as a byte or series of bytes; • to view serial output, click on the serial monitor at the arduino IDE: • More commands: arduino. cc/en/Reference/Home. Page Serial monitor

Hands-on Project 1: Talking to arduino 1 The hardware setup: 2 Write the code/sketch: Int Data = 0; void setup() { Serial. begin(9600); } void loop() { } if (Serial. available() > 0) { Data = Serial. read(); Serial. print("you have entered: "); Serial. write(Data); Serial. println(); }

Hands-on Project 1: Talking to arduino 3 Load the program into arduino 4 Open serial monitor and observe the output • Press verify button to check for errors • Press upload button to load the program into the arduino Serial monitor

Hands-on Project 2: LED Blinking 1 The hardware setup: 2 Write the code/sketch: void setup() { serial. begin(9600); pin. Mode(13, OUTPUT); } void loop() { digital. Write(13, HIGH); serial. println(“ LED ON”); delay(1000); digital. Write(13, LOW); serial. println(“ LED OFF”); delay(1000); }

Hands-on Project 2 : LED Blinking 3 Load the program into arduino 4 Observe the output on the board and open serial monitor • Press verify button to check for errors • Press upload button to load the program into the arduino Serial monitor

1 Hands-on Project 3: Serial controlled LED The hardware setup: 2 The code/sketch: void setup() { Serial. begin(9600); pin. Mode(13, OUTPUT); } void loop() { if (Serial. available() > 0) { int in. Byte = Serial. read(); switch (in. Byte) { case 'a': digital. Write(13, HIGH); break; case 'b': digital. Write(13, LOW); break; } } }

3 Hands-on Project 3 : Serial controlled LED Load the program into arduino • Press verify button to check for errors • Press upload button to load the program into the arduino 4 Press the letter ‘a’ or ‘b’ from your keyboard and observe your led

References www. arduino. cc www. ladyada. net/learn/arduino www. Earthshine. Electronics. com
- Slides: 22