Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth...

21
Arduino Programming IV Tim Woo

Transcript of Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth...

Page 1: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Arduino Programming IVTim Woo

Page 2: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

• We have to consider 6 components but not limited to these

Description Choices in this course

Abstract idea of project(Define the functionality of the system)

Many

Data format / representation Many

Programming Language C‐language

Communication Protocol Many

Physical connection (Pins assignment) Many

Hardware devices(Microcontroller, Peripherals)

Microcontroller: Arduino BoardPeripherals: Many

Products

Components

This is the part we have to fill up.

Design architecture of an embedded system

Ardunio Programming IV ‐ Tim Woo 2

Page 3: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Our ultimate goal

3Ardunio Programming IV ‐ Tim Woo

Page 4: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building blocks

Ardunio Programming IV ‐ Tim Woo 4

Bluetooth ChipsetHC05

Android Mobile Phone

Arduino boardUNO

H‐bridgeL298N

DC motor

DC motor

Page 5: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

• Traditionally, in a mixed hardware/software system, hardware and software are seenas independent

• Partitioning first, development afterward• In general, changes in hardware imply changes in software and vice versa• The overall verification is not done until the integration phase, which means that the

cost of detecting hardware/software errors is very high

Mixed Hardware / Software Co‐Development

Page 6: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block 1 

Ardunio Programming IV ‐ Tim Woo 6

PC Console

Serial Communication Routine

Challenge: What is the message to be received when you press a button on the mobile phone?

Bluetooth ChipsetHC06

Android Mobile Phone

Arduino boardUNO

Bluetooth Protocol

Page 7: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block 1 

Ardunio Programming IV ‐ Tim Woo 7

Description Choices in this course

Abstract idea of project(Define the functionality of the system)

Display the message to be received when you press a button on the mobile phone.

Data format / representation String / Character / text

Programming Language C‐language

Communication Protocol Bluetooth, Serial Communication

Physical connection (Pins assignment) Vcc, GND, Tx, Rx

Hardware devices(Microcontroller, Peripherals)

Microcontroller: Arduino BoardPeripherals: Mobile phone, Bluetooth, PC console

Page 8: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block 1

Ardunio Programming IV ‐ Tim Woo 8

Step 1: Connect the power signals between two boardsBluetooth         : Vcc &  GNDArduino Board : +3.3V / +5V  & GND

Question: Which one is connected (+3.3V / +5V)?

Bluetooth ChipsetHC06

Arduino boardUNO

Page 9: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block 1

Ardunio Programming IV ‐ Tim Woo 9

Step 2: Write the program to display the character onto the PC Console

Bluetooth ChipsetHC06

Arduino boardUNO

void setup() {// initialize the digital pin as an output.Serial.begin(9600);}

void loop() {if (Serial.available() > 0) {char input = Serial.read();Serial.print(input);

}}

Step 3: Compile the program and upload to the Arduino board

Initialization

Page 10: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block 1

Ardunio Programming IV ‐ Tim Woo 10

Step 4: Connect the power signals between two boardsBluetooth         : Tx &  RxArduino Board : Tx & Rx

Question: How do you connect them ? (Tx Rx) or (Tx Tx)

Bluetooth ChipsetHC06

Arduino boardUNO

Step 5: Pair‐up the mobile phone with the Bluetooth chipset

Step 6: Open the Serial Monitor

Page 11: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block 1

Ardunio Programming IV ‐ Tim Woo 11

Step 7: Press the screen button of the mobile phone

The screen shows

X0

“X” is the character / text when corresponding button is pressed.

“0” is indicated the end of string / end point.

Step 8: Record the text of all the buttons

BlueStick Control

Page 12: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block 2

Ardunio Programming IV ‐ Tim Woo 12

Arduino boardUNO

H‐bridgeL298N DC motor

Serial Communication Routine

Page 13: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building Block 2 – review H‐bridge circuit

Ardunio Programming IV ‐ Tim Woo 13

Ling Shi, ELEC125

DIR(Direction) ‐ +

+5V

PWM“Enable the power”

Connect to either 5V (high) or 0V (low)

High

Connected

Disconnected

Connected

Disconnected

Low

In1 In2

PWM1

Challenge: What happens if both IN1 and IN2 signals are the same?

Page 14: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building Block 2 – review H‐bridge circuit

Ardunio Programming IV ‐ Tim Woo 14

Ling Shi, ELEC125

DIR(Direction) ‐ +

+5V

PWM“Enable the power”

Connect to either 5V (high) or 0V (low)

Low

Challenge: Can you write down the status of the signals?

IN1 IN2

PWM1

Page 15: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building Block 2

Ardunio Programming IV ‐ Tim Woo 15

H‐bridgeL298N

DC motorStep 1: Connect a DC motor with L298N

DIR(Direction)

‐ +

+5V

PWM“Enable the power”

Connect to either 5V or 0V

In1 In2

IN1 IN2 IN3 IN4PWM1 PWM2

PWM1

Page 16: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block 2 

Ardunio Programming IV ‐ Tim Woo 16

Description Choices in this course

Abstract idea of project Connect the L298N and Arduino Board

Data format / representation

High / Low (direction),  Digital value 0‐255 (PWM)

Programming Language C‐language

Communication Protocol

Arduino Board – GPIO (General Purpose Input / Output)

Physical connection(Pins assignment)

L298N                 IN1       IN2        PWM1Arduino Board    ?            ?              ?

Hardware devices(Microcontroller, Peripherals)

Microcontroller: Arduino BoardPeripherals: L298N

Hint: The PWM signals can be generated through the pins, which are indicated with notation “~”. 

Challenge: Can we use GPIO0 and GPIO1 with IN1 and IN2 respectively?

Page 17: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building Block 2

Ardunio Programming IV ‐ Tim Woo 17

Step 2: Connect your designed pins between L298N and Arduino boardStep 3: Connect the power lines between the L298N and Arduino board

Step 4: Write the programInitialization :

void setup() {// put your setup code here, to run once:

}Implementation:

void loop() {// put your main code here, to run repeatedly

Arduino boardUNO

H‐bridgeL298N DC motor

Page 18: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block 2 ‐ initialization

Ardunio Programming IV ‐ Tim Woo 18

Description Choices in this course

Abstract idea of project Connect the L298N and Arduino Board

Data format / representation High / Low (direction),  Digital value 0‐255 (PWM)

Programming Language C‐languageInitialization Input/Output Input/Output Input/Output

Command: pinMode(pin_number, INPUT/OUTPUT)example:  pinMode(9,OUTPUT);

Challenges: Do you want to initial the status IN1 and IN2?What happens if both IN1 and IN2 are the same?

Command: digitalWrite(pin_number, HIGH/LOW)example:  digitalWrite(6,HIGH);

Challenges:  Do  you want to initial the PWM signal?Command: analogWrite(pin_number, value_of_PWM_signal)

example:  digitalWrite(9,0);

Arduino Board           ?                                    ?                                      ?L298N                         IN1                                 IN2                               PWM1

Page 19: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block 2 ‐ implementation

Command: digitalWrite(pin_number, HIGH/LOW);example:  digitalWrite(6,HIGH);

Command:  analogWrite(pin_number, value_of_PWM_signal);

example:  digitalWrite(9,0);

Command:  delay(time in ms) ;  ms = 1/1000 sec

example:  delay(5000);

Ardunio Programming IV ‐ Tim Woo 19

Example

Signals for IN1, In2Signal for PWM

Delay for 5000 ms (5 sec)

Initializationpins for IN1, IN2, PWM

0 2 4 6 8 10 120

100

200

300

400

500

600

700

Time (ms)

Spe

ed (r

pm)

Speed response of DC motor

Motor response w/o disturbancesMotor response w disturbancesDesired Speed

Settling time

Page 20: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Building block – putting two motors together

Ardunio Programming IV ‐ Tim Woo 20

Arduino boardUNO

H‐bridgeL298N

DC motor

Serial Communication Routine

Page 21: Arduino Programming IV · Arduino board UNO Step 5: Pair‐up the mobile phone with the Bluetooth chipset Step 6: Open the Serial Monitor. Building block 1 Ardunio Programming IV

Put the Building blocks together

Ardunio Programming IV ‐ Tim Woo 21

Bluetooth ChipsetHC05

Android Mobile Phone

Arduino boardUNO

H‐bridgeL298N

DC motor

DC motor