22
2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering http://fkee.uthm.edu.my/mfauzi [email protected] 018-4640050

2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

Embed Size (px)

Citation preview

Page 1: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

2.0 EMBEDDED CONTROLLER

Engr. Hj. Mohamad Fauzi bin ZakariaDepartment of Mechatronics and Robotics Engineering

Faculty of Electrical and Electronic Engineeringhttp://fkee.uthm.edu.my/mfauzi

[email protected] 018-4640050

Page 2: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 2

Course Contents

A. Microcontroller OverviewB. Arduino UnoC. Software SketchD. Application 1 – LED BlinkingE. Application 2 – LED and ButtonF. Application 3 – Analog InputG. Application 4 – LCD KeypadH. Demonstration – Line Following Mobile Robot

Page 3: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 3

Learning Outcome

At the end of this course the participant will be able to:• Recognize the variety of open microcontroller system. • Familiar with Arduino Uno microcontroller configuration.• Construct an application of Arduino-based microcontroller system.

Page 4: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 4

Microcontroller System

• What is a microcontroller?• It has Processor, ROM, RAM, and other peripheral such as input/output

digital, analog to digital convertor (ADC), timer/counter, pulse width modulation (PWM), interrupt, Universal Synchronous Asynchronous Receiver Transmitter (USART), inter-integrated circuit (I2C), serial peripheral interface (SPI).• Can be differentiate with processing size: 8, 16 and 32 bits.• Processing speed: Hz – needs internal or external clock generator to be

executed.

Page 5: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 5

Microcontroller Prototyping PlatformArduino (arduino.cc)

Pinguino (pinguino.cc)

Maple (leaflabs.com)

Sparkcore (spark.io)

mbed (mbed.org)

Raspberry pi (raspberrypi.org)

Beaglebone (beagleboard.org)

HARDWARE

SOFTWARE

follow Arduino

Technology

Page 6: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 6

Microcontroller Atmel AVR - ATmega328

Operating Voltage 5V

Input Voltage (recommended) 7-12V

Input Voltage (limits) 6-20V

Digital I/O Pins 14 (of which 6 provide PWM output)

Analog Input Pins 6

DC Current per I/O Pin 40 mA

DC Current for 3.3V Pin 50 mA

Flash Memory 32 KB (ATmega328) of which 0.5 KB used by bootloader

SRAM 2 KB (ATmega328)

EEPROM 1 KB (ATmega328)

Clock Speed 16 MHz

Length 68.6 mm

Width 53.4 mm

Weight 25 g

Arduino Uno

Page 9: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 9

Software SketchA sketch: uses for a program by Arduino community that contains the unit of code would be uploaded to and run on an Arduino board.

It relies on C language.

Therefore, we have to familiar on how to create

• Structure• Variables • Functions or subroutines

http://arduino.cc/en/Referenceor

Refer to Arduino Cheat Sheet

Page 10: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 10

Application 1 – LED Blinking

1. Problem Definition: Construct a system for LED blinking (ON and OFF) every 1s on D13 pin of Arduino Uno microcontroller

2. Knowledge needed: Sinking or sourcing mode for digital output interface.

3. Identify the suitable components and construct its circuit.4. Need to use Digital Output and Delay Functions:

pinMode(pin,direction)digitalWrite(pin,value)delay(value in ms)

using Sequential Programming method

Page 11: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 11

Application 2 – LED and Button

1. Problem Definition: Create a system for LED ON when a button is pressed and LED OFF whenever the button is released by using Arduino Uno microcontroller.

2. Knowledge needed: Sinking or sourcing mode for digital output interface and pull-up or down resistor for digital input interface.

3. Identify the suitable components and construct its circuit.4. Need to use Digital I/O Functions:

pinMode(pin,direction)digitalWrite(pin,value)digitalRead(pin)

using Sequential Programming method

Page 12: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 12

Application 3 – Analog Input

1. Problem Definition: Construct a temperature indication system which has LM35 that connected to an analog pin of Arduino Uno microcontroller. When temperature reading over than 30°C, a LED indicator will be ON.

2. Knowledge needed: ADC and digital output interface mode.3. Identify the suitable components and construct its circuit.4. Need to use Analog Input and Digital I/O Functions:

pinMode(pin,direction)digitalWrite(pin,value)analogRead(pin)

using Sequential Programming method

Page 13: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 13

Application 4 – LCD Keypad

1. Problem Definition: Develop an user interface shield which consists of an Alphanumeric LCD and analog keypad for Arduino Uno microcontroller.

2. Knowledge needed: Analog keypad input and LCD interface.3. Identify the suitable components and construct its circuit.4. Need to use Analog Input and LCD Functions:

#include <LiquidCrystal.h>LiquidCrystal(rs, enable, d4, d5, d6, d7)lcd.begin(cols, rows)lcd.setCursor(col, row) lcd.print(data)analogRead(pin)

Further Informationhttp://www.dfrobot.com/wiki/index.php/LCD_KeyPad_Shield_For_Arduino_SKU:_DFR0009

using Sequential Programming method

Page 14: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 14

Arrow-botA Teaching Tool

A line following differential

wheel mobile robot

Label Components1 5 Voltage Battery Supply2 L293DNE Motor Driver3 Power ON/OFF Switch4 LED Indicator5 2 x Motors DC6 Programmer Pin7 Inter-integrated Communication 8 ATmega328P with Arduino Uno Bootloader9 Serial Peripheral Interface

10 Start Button11 Reset Button

TOP VIEW

BOTTOM VIEW

Page 15: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 15

System Architecture

Arduino Pin Assignment

Page 17: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 17

PCB – using Proteus Ares

Page 18: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 18

Software Algorithm (Procedure)

1. setup()1. Configure output and input pins2. Wait for user button activation. During waiting

activity, read analog sensor data and send to UART for debugging purpose.

2. loop()1. Read or update analog sensor data2. Convert analog sensor data to Boolean data and

update LED indicator3. Control motor based on Boolean data as in the

table.

MOVEMENT IRL IRM IRRStraight 1

011

10

Left 1 1 0Heavy Left 1 0 0

Right 0 1 1Heavy Right 0 0 1

Stop 0 0 0

using Sequential Programming method

Page 19: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 19

Arduino Sketch = Program Code

#define LEDL 7#define LEDM 4#define LEDR 8

#define IRL 1#define IRM 2#define IRR 3

#define LEDL_ON digitalWrite(LEDL,HIGH)#define LEDM_ON digitalWrite(LEDM,HIGH)#define LEDR_ON digitalWrite(LEDR,HIGH)#define LEDL_OFF digitalWrite(LEDL,LOW)#define LEDM_OFF digitalWrite(LEDM,LOW)#define LEDR_OFF digitalWrite(LEDR,LOW)

boolean IRL_STATUS, IRM_STATUS, IRR_STATUS; //status for line detection or not.

using Sequential Programming method

Page 20: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 20

void Motor(uint8_t speed1, uint8_t speed2) { speed1=map(speed1,0,100,0,255); speed2=map(speed2,0,100,0,255); //=======================MOTOR1 analogWrite(3,0); //PWM1 analogWrite(5,speed1); //PWM2 //=======================MOTOR2 analogWrite(6,speed2); //PWM3 analogWrite(9,0); //PWM4}

void setup() { pinMode(LEDL,OUTPUT); pinMode(LEDM,OUTPUT); pinMode(LEDR,OUTPUT); pinMode(2,INPUT); Serial.begin(9600); while(digitalRead(2)==HIGH) //Wait for user button and used for debugging { Serial.print(analogRead(IRL)); Serial.print("\t"); Serial.print(analogRead(IRM)); Serial.print("\t"); Serial.println(analogRead(IRR)); }}

using Sequential Programming method

Page 21: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 21

void loop() { //READ SENSOR======================================== int data_IRL = analogRead(IRL); int data_IRM = analogRead(IRM); int data_IRR = analogRead(IRR); //UPDATE SENSOR STATUS AND LED INDICATOR============= if((data_IRL > 154)&&(data_IRL < 460)) {IRL_STATUS=HIGH; LEDL_ON;} else {IRL_STATUS=LOW; LEDL_OFF;} if((data_IRM > 154)&&(data_IRM < 460)) {IRM_STATUS=HIGH; LEDM_ON;} else {IRM_STATUS=LOW; LEDM_OFF;} if((data_IRR > 154)&&(data_IRR < 460)) {IRR_STATUS=HIGH; LEDR_ON;} else {IRR_STATUS=LOW; LEDR_OFF;} //MOTOR CONTROL====================================== if((IRL_STATUS&&IRM_STATUS&&IRR_STATUS)||(!IRL_STATUS&&IRM_STATUS&&!IRR_STATUS)) Motor(100,70);

//STRAIGHT else if(IRL_STATUS&&IRM_STATUS&&!IRR_STATUS) Motor(60,70); //MOVE LEFT else if(IRL_STATUS&&!IRM_STATUS&&!IRR_STATUS) Motor(0,70); //MOVE HEAVY LEFT else if(!IRL_STATUS&&IRM_STATUS&&IRR_STATUS) Motor(100,40); //MOVE RIGHT else if(!IRL_STATUS&&!IRM_STATUS&&IRR_STATUS) Motor(100,0); //MOVE HEAVY RIGHT else if(!IRL_STATUS&&!IRM_STATUS&&!IRR_STATUS) Motor(0,0); //STOP}

using Sequential Programming method

Page 22: 2.0 EMBEDDED CONTROLLER Engr. Hj. Mohamad Fauzi bin Zakaria Department of Mechatronics and Robotics Engineering Faculty of Electrical and Electronic Engineering

[email protected] 22

Conclusion

Hope that you have:• Know the variety of open microcontroller system available in the

market. • Familiar with Arduino Uno microcontroller pin mapping configuration.• {Main business} Confident in designing, developing and testing any

application of Arduino-based microcontroller system by referring closely in Arduino website. • Tips for starting the system design are draw the system architecture,

identify the microcontroller pin assignment and draw software algorithm before sketch the program.