Contents
List of Figures v
List of Tables ix
List of Arduino Code xi
List of Scilab Code xiii
List of Acronyms xv
1 Introduction 1
2 Hardware Environment 3
2.1 Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1.1 Organization of a Microcontroller . . . . . . . . . . . . . . . . 3
2.1.2 Microcontroller Peripherals . . . . . . . . . . . . . . . . . . . 5
2.2 Open Source Hardware (OSHW) . . . . . . . . . . . . . . . . . . . . 7
2.3 Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.1 Brief History . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.2 Arduino Uno Board . . . . . . . . . . . . . . . . . . . . . . . 9
2.3.3 Popular Arduino Projects . . . . . . . . . . . . . . . . . . . . 9
2.4 Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.5 Experimental Test Bed . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3 Software Environment 17
3.1 Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.1.1 Downloading and installing on Windows . . . . . . . . . . . . 18
3.1.2 Downloading and installing on GNU/Linux Ubuntu . . . . . . 18
3.1.3 Arduino Development Environment . . . . . . . . . . . . . . . 21
3.1.4 Testing Arduino with a sample program . . . . . . . . . . . . 23
ii Contents
3.2 Scilab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.2.1 Downloading and installing Scilab . . . . . . . . . . . . . . . 24
3.2.2 Scilab Arduino toolbox . . . . . . . . . . . . . . . . . . . . . . 25
3.2.3 Identifying Arduino communication port number . . . . . . . 27
3.2.4 Testing Scilab-Arduino toolbox . . . . . . . . . . . . . . . . . 29
3.3 Xcos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.3.1 Downloading, installing and testing . . . . . . . . . . . . . . . 33
3.3.2 Use case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.3.3 Xcos-Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.4 Arduino Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.5 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4 Interfacing a Light Emitting Diode 41
4.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.2 Lighting the LED from the Arduino IDE . . . . . . . . . . . . . . . . 43
4.3 Lighting the LED from Scilab Scripts . . . . . . . . . . . . . . . . . . 45
4.4 Lighting the LED from Scilab Xcos . . . . . . . . . . . . . . . . . . . 47
4.5 Arduino Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.6 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5 Interfacing a Pushbutton 55
5.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.2 Reading the Pushbutton status from the Arduino IDE . . . . . . . . 55
5.3 Reading the Pushbutton status from Scilab Scripts . . . . . . . . . . 57
5.4 Accessing the Pushbutton from Xcos . . . . . . . . . . . . . . . . . . 58
5.5 Arduino Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.6 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
6 Interfacing a Light Dependent Resistor 63
6.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
6.2 Arduino Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.3 Scilab Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
6.4 LDR experiments through Xcos . . . . . . . . . . . . . . . . . . . . . 67
6.5 Arduino Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
6.6 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
7 Interfacing a DC motor 73
7.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.2 Controlling the DC motor from Arduino . . . . . . . . . . . . . . . . 75
7.3 Controlling the DC motor from Scilab . . . . . . . . . . . . . . . . . 77
Contents iii
7.3.1 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7.3.2 Rotation for a specified time . . . . . . . . . . . . . . . . . . 77
7.3.3 Using the capabilities of Scilab . . . . . . . . . . . . . . . . . 79
7.4 Driving the DC motor from Xcos . . . . . . . . . . . . . . . . . . . . 79
7.5 Arduino Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.6 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
8 Interfacing a Potentiometer 87
8.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
8.2 Reading the potentiometer from the Arduino IDE . . . . . . . . . . . 88
8.3 Reading the potentiometer from Scilab Script . . . . . . . . . . . . . 89
8.4 Reading the potentiometer from Scilab Xcos . . . . . . . . . . . . . . 90
8.5 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
8.6 Arduino Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
8.7 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
9 Interfacing a Thermistor 95
9.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
9.2 Reading thermistor from Arduino IDE . . . . . . . . . . . . . . . . . 96
9.3 Reading thermistor from Scilab scripts . . . . . . . . . . . . . . . . . 98
9.4 Reading thermistor from Xcos . . . . . . . . . . . . . . . . . . . . . . 99
9.5 Arduino Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
9.6 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
10 Interfacing a Servomotor 105
10.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
10.2 Control through Arduino IDE . . . . . . . . . . . . . . . . . . . . . . 106
10.3 Control through Scilab Scripts . . . . . . . . . . . . . . . . . . . . . . 107
10.4 Control through Xcos code . . . . . . . . . . . . . . . . . . . . . . . . 108
10.5 Arduino Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
10.6 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
11 Implementation of Modbus Protocol 117
11.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
11.2 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
11.3 Energy Meter set up for Modbus protocol with Arduino Uno . . . . . 122
11.4 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
11.5 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
11.6 Reading Parameters from Xcos . . . . . . . . . . . . . . . . . . . . . 126
11.6.1 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . 126
iv Contents
11.7 Arduino Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
11.8 Scilab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
References 133
List of Figures
2.1 Functional block diagram of a microcontroller . . . . . . . . . . . . . 4
2.2 ADC resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 The logo of Open Source Hardware . . . . . . . . . . . . . . . . . . . 7
2.4 Arduino Uno Board . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5 Arduino Mega Board . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6 LilyPad Arduino Board . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.7 Arduino Phone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.8 3D printer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.9 PCB image of the shield . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.10 Pictorial representation of the schematic of the shield . . . . . . . . . 14
2.11 PCB of the shield is this wording ok? . . . . . . . . . . . . . . . . . . 15
2.12 Picture of the shield with all components . . . . . . . . . . . . . . . . 15
3.1 Windows device manager . . . . . . . . . . . . . . . . . . . . . . . . 19
3.2 Windows update driver option . . . . . . . . . . . . . . . . . . . . . . 20
3.3 Confirmation for executing Arduino script . . . . . . . . . . . . . . . 21
3.4 Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.5 Linux terminal to launch Scilab . . . . . . . . . . . . . . . . . . . . . 25
3.6 Changing scilab directory . . . . . . . . . . . . . . . . . . . . . . . . 26
3.7 Browsing toolbox directory . . . . . . . . . . . . . . . . . . . . . . . 27
3.8 Output of builder.sce . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.9 Output of loader.sce . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.10 Device Manager in windows . . . . . . . . . . . . . . . . . . . . . . . 30
3.11 COM port properties window . . . . . . . . . . . . . . . . . . . . . . 31
3.12 Port number on Linux terminal . . . . . . . . . . . . . . . . . . . . . 31
3.13 Scilab test code output . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.14 Arduino toolbox functions used in this book . . . . . . . . . . . . . . 33
3.15 Sine generator in palette browser . . . . . . . . . . . . . . . . . . . . 34
3.16 CSCOPE block in xcos . . . . . . . . . . . . . . . . . . . . . . . . . . 35
v
vi List of Figures
3.17 CLOCK_c block in xcos . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.18 Sine generator in Xcos . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.19 Sine generator Xcos output . . . . . . . . . . . . . . . . . . . . . . . 37
3.20 CSCOPE configuration window . . . . . . . . . . . . . . . . . . . . . 37
3.21 Simulation setup window . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.22 Palette browser showing Arduino blocks . . . . . . . . . . . . . . . . 39
3.23 Xcos block help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.1 Light Emitting Diode . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.2 Internal connection diagram for LED on the shield . . . . . . . . . . 42
4.3 Connecting Arduino and Shield . . . . . . . . . . . . . . . . . . . . . 42
4.4 LED experiments directly on Arduino Uno board, without the shield 45
4.5 Turning the blue LED on through Xcos . . . . . . . . . . . . . . . . 47
4.6 Turning the blue LED on through Xcos for two seconds . . . . . . . 48
4.7 Turning the blue and red LEDs on through Xcos and turning them
off one by one . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.8 Blinking the green LED every second through Xcos . . . . . . . . . . 51
5.1 Connection Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.2 Printing the push button status on the display block . . . . . . . . . 58
5.3 Turning the LED on or off, depending on the pushbutton . . . . . . 59
6.1 Light Dependent Resistor . . . . . . . . . . . . . . . . . . . . . . . . 64
6.2 Internal connection diagram for the LDR on the shield . . . . . . . . 64
6.3 Xcos diagram to read LDR values . . . . . . . . . . . . . . . . . . . . 67
6.4 Xcos diagram to read the value of the LDR, which is used to turn the
blue LED on or off . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
7.1 L293D motor driver board . . . . . . . . . . . . . . . . . . . . . . . . 74
7.2 A schematic of DC motor connections . . . . . . . . . . . . . . . . . 75
7.3 How to connect the DC motor to the Arduino Uno board . . . . . . 75
7.4 Control of DC motor for a specified time from Xcos . . . . . . . . . . 80
7.5 Xcos control of the DC motor in forward and reverse directions . . . 81
7.6 Xcos control of the DC motor in forward and reverse directions . . . 82
8.1 Potentiometer’s schematic on the shield . . . . . . . . . . . . . . . . 88
8.2 Turning LEDs on through Xcos depending on the potentiometer thresh-
old . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
9.1 Pictorial and symbolic representation of a thermistor . . . . . . . . . 96
9.2 Thermistor and buzzer connection diagrams . . . . . . . . . . . . . . 96
List of Figures vii
9.3 Xcos diagram to read thermistor values . . . . . . . . . . . . . . . . 100
9.4 Output of Xcos diagram to read thermistor values . . . . . . . . . . 101
9.5 Xcos diagram to read the value of thermistor, which is used to turn
the buzzer on or off . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
9.6 Output of Xcos diagram to switch buzzer through thermistor values 102
10.1 Rotating the servomotor by a fixed angle . . . . . . . . . . . . . . . . 109
10.2 Rotating the servomotor forward and then reverse . . . . . . . . . . 110
10.3 Rotating the servomotor in increments of 20◦ . . . . . . . . . . . . . 111
10.4 Rotating the servomotor as suggested by the potentiometer . . . . . 112
11.1 Block diagram representation of the Protocol . . . . . . . . . . . . . 118
11.2 Master-Slave Query-Response Cycle . . . . . . . . . . . . . . . . . . 118
11.3 Pins in RS485 module . . . . . . . . . . . . . . . . . . . . . . . . . . 119
11.4 MODBUS Set Up for Energy Meter . . . . . . . . . . . . . . . . . . . 123
11.5 Block Diagram for Energy Meter Setup . . . . . . . . . . . . . . . . . 124
11.6 Flowchart of Arduino firmware . . . . . . . . . . . . . . . . . . . . . 125
11.7 Flow Chart of the Modbus Energy Meter Implementation . . . . . . 126
11.8 Single Phase Current Output on Scilab Console . . . . . . . . . . . . 127
11.9 Single Phase Current Output on Energy Meter . . . . . . . . . . . . 127
11.10Single Phase Voltage Output on Scilab Console . . . . . . . . . . . . 128
11.11Single Phase Voltage Output on Energy Meter . . . . . . . . . . . . 128
11.12Single Phase Voltage Output on Scilab Console . . . . . . . . . . . . 129
11.13Single Phase Voltage Output on Energy Meter . . . . . . . . . . . . 129
11.14Xcos diagram to read Energy Meter values . . . . . . . . . . . . . . . 130
viii List of Figures