[Virtual Presenter] School of Engineering Reviewed Jan 2025.
[Audio] Table of Contents Competence 1: Configure Microcontroller interface............................................................. 1 Unit 1 (C1TK1): System Components and Interface of Microcontroller ........................... 1 1.1 Microcontroller ........................................................................................................ 1 1.2 Components and Interface of Arduino UNO Microcontroller ................................ 2 Unit 2 (C1TK2): Common Syntax and Programming Style .................................................. 1 2.1 Sketch ...................................................................................................................... 1 2.2 Structure................................................................................................................... 1 2.3 Main Function.......................................................................................................... 1 2.4 Data Types ............................................................................................................... 2 2.5 Variables & Constants ............................................................................................. 6 2.6 Control Statements .................................................................................................. 8 2.7 Function Declaration ............................................................................................... 8 Unit 3 (C1TK3): Program Compilation and Uploading ........................................................ 1 3.1 Connection Cable .................................................................................................... 1 3.2 Download Arduino IDE Software ........................................................................... 1 3.3 Power up the board. ................................................................................................. 2 3.4 Launch Arduino IDE. .............................................................................................. 2 3.5 Open the first project. .............................................................................................. 2 3.6 Select the Arduino board. ........................................................................................ 3 3.7 Select the serial port. ............................................................................................... 4 3.8 Upload the program to the board. ............................................................................ 4 Unit 4 (C1TK4): Common Programming Errors ................................................................... 1 4.1 Common Programing Errors ................................................................................... 1 4.2 Serial Nonsense ....................................................................................................... 2 4.3 Missing Libraries. .................................................................................................... 3 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices .. 1 5.1 Using a Switch ......................................................................................................... 2 5.2 Using a Switch without External Resistors ............................................................. 5 5.3 Reliably Detecting the Closing of a Switch............................................................. 7 Unit 6 (C1TK6): Requirement of Input and Output Devices................................................. 1 6.1 Pins Configured as INPUT ............................................................................................ 1 6.2 Using Built-in Pull-up Resistor with Pins Configured as Input .............................. 3 6.3 Pins Configured as OUTPUT .................................................................................. 3 6.4 Pins Configured as OUTPUT (PWM) ..................................................................... 4 Unit 7 (C1TK7): Assigning the Pin Mode for IO Pins .......................................................... 1.
[Audio] 7.1 digitalWrite() Function ............................................................................................ 2 7.2 analogRead() Function ............................................................................................ 2 Unit 8 (C1TK8): Types of Input Devices .............................................................................. 1 8.1 Switches ................................................................................................................... 1 8.2 Resistors................................................................................................................... 2 8.3 Voltage Regulators .................................................................................................. 3 8.4 Sensors ..................................................................................................................... 5 Unit 9 (C1TK9): Types of Output Devices............................................................................ 1 9.1 LED ......................................................................................................................... 1 9.2 Motor ....................................................................................................................... 1 9.1 Buzzers and Speakers .............................................................................................. 4 9.2 Transistor ................................................................................................................. 4 9.3 LCD Display ............................................................................................................ 5 Competence 2: Interfacing with Sensors and IO Devices......................... 错误!未定义书签。 Unit 10 (C2TK1): Electronic Sensors .................................................................................... 1 10.1 Different Types of Sensors .................................................................................. 1 10.2 Temperature Sensor ............................................................................................. 2 10.3 Pressure sensor ..................................................................................................... 3 10.4 Level sensors ........................................................................................................ 5 10.5 Sensor Application ............................................................................................... 8 Unit 11(C2TK2): Installing Sensors ...................................................................................... 1 11.1 Sensors Installation .............................................................................................. 1 Unit 12 (C2TK3): Constraint on Sensors Installation............................................................ 1 12.1 Temperature Sensor for Indoor Installation. ........................................................ 1 12.2 Temperature Sensor for Outdoor Installation. ..................................................... 2 12.3 Humidity Sensor for Outdoor Installation. .......................................................... 4 12.4 Humidity Sensor for Indoor Installation. ............................................................. 5 12.5 Pressure Sensor General Installation.................................................................... 6 12.6 Level Sensor Installation Constraint. ................................................................... 6 Unit 13 (C2TK4): Ingress Protection ..................................................................................... 1 13.1 Products That Require IP Rating ......................................................................... 1 13.2 Understanding the IP Rating ................................................................................ 2 13.3 Examples on IP Rating Interpretation. ................................................................. 5 Competence 3: Applying IOT Technology................................................................................ 1 Unit 14 (C3TK1): IOT Applications...................................................................................... 1 14.1 Components of the IOT........................................................................................ 1 14.2 Application of IOT ............................................................................................... 1.
[Audio] Unit 15 (C3TK2): IOT Software System Platform ................................................................ 1 15.1 Data Ingestion ...................................................................................................... 1 15.2 Data Processing .................................................................................................... 2 15.3 Application ........................................................................................................... 3 Unit 16 (C3TK3): Transfer Protocols for Data Transmission ............................................... 1 16.1 HTTP.................................................................................................................... 1 16.2 MQTT .................................................................................................................. 2.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Competence 1: Configure Microcontroller interface Unit 1 (C1TK1): System Components and Interface of Microcontroller Introduction The Internet of things (IoT) is the network of physical devices, vehicles, home appliances, and other items embedded with electronics, software, sensors, actuators, and connectivity which enables these things to connect, collect and exchange data creating opportunities for more direct integration of the physical world into computer-based systems, resulting in efficiency improvements, economic benefits, and reduced human exertion 1.1 Microcontroller A microcontroller is a small computer on a single integrated circuit. Like any computer, it has memory, and can be programmed to do calculations, receive input, and generate output. Unlike a PC, it incorporates memory, a CPU, peripherals and I/O interfaces into a single chip. Microcontrollers are found in almost every electronic device. They are used in automatically controlled products and devices, such as automobile engine control systems, implantable medical devices, remote controls, office machines, appliances, power tools, toys, ovens and others. 1-1 Unit 1 (C1TK1): Identify major system components and interface of a Microcontroller.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 1.2 Components and Interface of Arduino UNO Microcontroller USB Port Arduino board can be powered by using the USB cable from your computer. All you need to do is connect the USB cable to the USB connection (1). Power (Barrel Jack) Arduino boards can be powered directly from the AC mains power supply by connecting it to the Barrel Jack (2). Voltage Regulator The function of the voltage regulator is to control the voltage given to the Arduino board and stabilize the DC voltages used by the processor and other elements. 1-2 Unit 1 (C1TK1): Identify major system components and interface of a Microcontroller.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Crystal Oscillator The crystal oscillator helps Arduino in dealing with time issues. How does Arduino calculate time? The answer is, by using the crystal oscillator. The number printed on top of the Arduino crystal is 16.000H9H. It tells us that the frequency is 16,000,000 Hertz or 16 MHz. Arduino Reset You can reset your Arduino board, i.e., start your program from the beginning. You can reset the UNO board in two ways. First, by using the reset button (17) on the board. Second, you can connect an external reset button to the Arduino pin labelled RESET (5). Pins (3.3, 5, GND, Vin) 3.3V (6) − Supply 3.3 output volt 5V (7) − Supply 5 output volt Most of the components used with Arduino board works fine with 3.3 volt and 5 volt. GND (8) (Ground) − There are several GND pins on the Arduino, any of which can be used to ground your circuit. Vin (9) − This pin also can be used to power the Arduino board from an external power source, like AC mains power supply. Analog pins The Arduino UNO board has six analog input pins A0 through A5. These pins can read the signal from an analog sensor like the humidity sensor or temperature sensor and convert it into a digital value that can be read by the microprocessor. Main microcontroller Each Arduino board has its own microcontroller (11). You can assume it as the brain of your board. The main IC (integrated circuit) on the Arduino is slightly different from board to board. The microcontrollers are usually of the ATMEL Company. You must know what IC your board has before loading up a new program from the Arduino IDE. This information is 1-3 Unit 1 (C1TK1): Identify major system components and interface of a Microcontroller.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 available on the top of the IC. For more details about the IC construction and functions, you can refer to the data sheet. ICSP pin Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting of MOSI, MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial Peripheral Interface), which could be considered as an "expansion" of the output. Actually, you are slaving the output device to the master of the SPI bus. Power LED indicator This LED should light up when you plug your Arduino into a power source to indicate that your board is powered up correctly. If this light does not turn on, then there is something wrong with the connection. TX and RX LEDs On your board, you will find two labels: TX (transmit) and RX (receive). They appear in two places on the Arduino UNO board. First, at the digital pins 0 and 1, to indicate the pins responsible for serial communication. Second, the TX and RX led (13). The TX led flashes with different speed while sending the serial data. The speed of flashing depends on the baud rate used by the board. RX flashes during the receiving process. Digital I/O The Arduino UNO board has 14 digital I/O pins (15) (of which 6 provide PWM (Pulse Width Modulation) output. These pins can be configured to work as input digital pins to read logic values (0 or 1) or as digital output pins to drive different modules like LEDs, relays, etc. The pins labeled "~" can be used to generate PWM. AREF AREF stands for Analog Reference. It is sometimes, used to set an external reference voltage (between 0 and 5 Volts) as the upper limit for the analog input pins. 1-4 Unit 1 (C1TK1): Identify major system components and interface of a Microcontroller.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Unit 2 (C1TK2): Common Syntax and Programming Style 2.1 Sketch The first new terminology is the Arduino program called "sketch". 2.2 Structure Arduino programs can be divided in three main parts: Structure, Values (variables and constants), and Functions. We will learn about the Arduino software program, step by step, and how we can write the program without any syntax or compilation error. 2.3 Main Function Software structure consist of two main functions − Setup( ) function Loop( ) function 2-1 Unit 2 (C1TK2): Explain common syntax and programming style of High Level Programming Language.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Void setup ( ) PURPOSE − The setup() function is called when a sketch starts. Use it to initialize the variables, pin modes, start using libraries, etc. The setup function will only run once, after each power up or reset of the Arduino board. INPUT − - OUTPUT − - RETURN − - Void Loop ( ) PURPOSE − After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board. INPUT − - OUTPUT − - RETURN − - 2.4 Data Types The following table provides all the data types that you will use during Arduino programming. void Boolean char Unsigned byte int Unsigned int word char long Unsigned short float double array String-char Stringlong array object void The void keyword is used only in function declarations. It indicates that the function is expected to return no information to the function from which it was called. Example Void Loop ( ) 2-2 Unit 2 (C1TK2): Explain common syntax and programming style of High Level Programming Language.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Boolean A Boolean holds one of two values, true or false. Each Boolean variable occupies one byte of memory. Example boolean val = false ; // declaration of variable with type boolean and initialize it with false boolean state = true ; // declaration of variable with type boolean and initialize it with true Char A data type that takes up one byte of memory that stores a character value. Character literals are written in single quotes like this: 'A' and for multiple characters, strings use double quotes: "ABC". However, characters are stored as numbers. You can see the specific encoding in the ASCII chart. This means that it is possible to do arithmetic operations on characters, in which the ASCII value of the character is used. For example, 'A' + 1 has the value 66, since the ASCII value of the capital letter A is 65. Example Char chr_a = 'a' ;//declaration of variable with type char and initialize it with character a Char chr_c = 97 ;//declaration of variable with type char and initialize it with character 97 Unsigned Char 2-3 Unit 2 (C1TK2): Explain common syntax and programming style of High Level Programming Language.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Unsigned char is an unsigned data type that occupies one byte of memory. The unsigned char data type encodes numbers from 0 to 255. Example Unsigned Char chr_y = 121 ; // declaration of variable with type Unsigned char and initialize it with character y byte A byte stores an 8-bit unsigned number, from 0 to 255. Example byte m = 25 ;//declaration of variable with type byte and initialize it with 25 int Integer Integers are the primary data-type for number storage. int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). The int size varies from board to board. On the Arduino Due, for example, an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1). Example int counter = 32 ;// declaration of variable with type int and initialize it with 32 Unsigned int Unsigned ints (unsigned integers) are the same as int in the way that they store a 2 byte value. Instead of storing negative numbers, however, they only store positive values, yielding a useful range of 0 to 65,535 (2^16) - 1). The Due stores a 4 byte (32-bit) value, ranging from 0 to 4,294,967,295 (2^32 - 1). Example Unsigned int counter = 60 ; // declaration of variable with type unsigned int and initialize it with 60 Word On the Uno and other ATMEGA based boards, a word stores a 16-bit unsigned number. On the Due and Zero, it stores a 32-bit unsigned number. Example word w = 1000 ;//declaration of variable with type word and initialize it with 1000 Long 2-4 Unit 2 (C1TK2): Explain common syntax and programming style of High Level Programming Language.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647. Example Long velocity = 102346 ;//declaration of variable with type Long and initialize it with 102346 unsigned long Unsigned long variables are extended size variables for number storage and store 32 bits (4 bytes). Unlike standard longs, unsigned longs will not store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1). Example Unsigned Long velocity = 101006 ;// declaration of variable with type Unsigned Long and initialize it with 101006 short A short is a 16-bit data-type. On all Arduinos (ATMega and ARM based), a short stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). Example short val = 13 ;//declaration of variable with type short and initialize it with 13 float Data type for floating-point number is a number that has a decimal point. Floatingpoint numbers are often used to approximate the analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38 and as low as 3.4028235E+38. They are stored as 32 bits (4 bytes) of information. Example float num = 1.352;//declaration of variable with type float and initialize it with 1.352 double On the Uno and other ATMEGA based boards, Double precision floating-point number occupies four bytes. That is, the double implementation is exactly the same as the float, with no gain in precision. On the Arduino Due, doubles have 8-byte (64 bit) precision. Example double num = 45.352 ;// declaration of variable with type double and initialize it with 45. 2-5 Unit 2 (C1TK2): Explain common syntax and programming style of High Level Programming Language.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 2.5 Variables & Constants A scope is a region of the program and there are three places where variables can be declared. They are − Inside a function or a block, which is called local variables. In the definition of function parameters, which is called formal parameters. Outside of all functions, which is called global variables. Local Variables Variables that are declared inside a function or block are local variables. They can be used only by the statements that are inside that function or block of code. Local variables are not known to function outside their own. Following is the example using local variables – 2-6 Unit 2 (C1TK2): Explain common syntax and programming style of High Level Programming Language.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Global Variables Global variables are defined outside of all the functions, usually at the top of the program. The global variables will hold their value throughout the life-time of your program. A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. The following example uses global and local variables − 2-7 Unit 2 (C1TK2): Explain common syntax and programming style of High Level Programming Language.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 2.6 Control Statements Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program. It should be along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Control Statements are elements in Source Code that control the flow of program execution. They are − S.NO. Control Statement & Description If statement It takes an expression in parenthesis and a statement or block of statements. 1 If the expression is true then the statement or block of statements gets executed otherwise these statements are skipped. If …else statement An if statement can be followed by an optional else statement, which executes 2 when the expression is false. If…else if …else statement The if statement can be followed by an optional else if...elsestatement, which 3 is very useful to test various conditions using single if...else if statement. switch case statement Similar to the if statements, switch...case controls the flow of programs by 4 allowing the programmers to specify different codes that should be executed in various conditions. Conditional Operator ? : 5 The conditional operator ? : is the only ternary operator in C. 2.7 Function Declaration A function is declared outside any other functions, above or below the loop function. 2-8 Unit 2 (C1TK2): Explain common syntax and programming style of High Level Programming Language.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 We can declare the function in two different ways − The first way is just writing the part of the function called a function prototype above the loop function, which consists of − Function return type Function name Function argument type, no need to write the argument name Function prototype must be followed by a semicolon ( ; ). Example The second part, which is called the function definition or declaration, must be declared below the loop function, which consists of − Function return type Function name Function argument type, here you must add the argument name The function body (statements inside the function executing when the function is called) Time Arduino provides four different time manipulation functions. They are − S.No. Function & Description 1 delay () function 2-9 Unit 2 (C1TK2): Explain common syntax and programming style of High Level Programming Language.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 The way the delay() function works is pretty simple. It accepts a single integer (or number) argument. This number represents the time (measured in milliseconds). delayMicroseconds () function The delayMicroseconds() function accepts a single integer (or number) 2 argument. There are a thousand microseconds in a millisecond, and a million microseconds in a second. millis () function This function is used to return the number of milliseconds at the time, the 3 Arduino board begins running the current program. micros () function The micros() function returns the number of microseconds from the time, 4 the Arduino board begins running the current program. This number overflows i.e. goes back to zero after approximately 70 minutes. 2-10 Unit 2 (C1TK2): Explain common syntax and programming style of High Level Programming Language.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Unit 3 (C1TK3): Program Compilation and Uploading In this section, we will learn in easy steps, how to set up the Arduino IDE on our computer and prepare the board to receive the program via USB cable. 3.1 Connection Cable Step 1 − First you must have your Arduino board and a USB cable. In case you use Arduino UNO, you will need a standard USB cable (A plug to B plug), the kind you would connect to a USB printer as shown in the following image. 3.2 Download Arduino IDE Software Step 2 You can get different versions of Arduino IDE from the Download page on the Arduino Official website. You must select your software, which is compatible with your operating system (Windows, IOS, or Linux). After your file download is complete, unzip the file. 3-1 Unit 3 (C1TK3): Program Compilation and Uploading.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 3.3 Power up the board. Step 3 The Arduino Uno automatically draw power from either, the USB connection to the computer or an external power supply. The power source is selected with a jumper, a small piece of plastic that fits onto two of the three pins between the USB and power jacks. Check that it is on the two pins closest to the USB port. Connect the Arduino board to your computer using the USB cable. The green power LED (labeled PWR) should glow. 3.4 Launch Arduino IDE. Step 4 After your Arduino IDE software is downloaded, you need to unzip the folder. Inside the folder, you can find the application icon with an infinity label (application.exe). Double-click the icon to start the IDE. 3.5 Open the first project. Step 5 Once the software starts, you have two options − Create a new project. Open an existing project example. 3-2 Unit 3 (C1TK3): Program Compilation and Uploading.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 To create a new project, select File → New. To open an existing project example, select File → Example → Basics → Blink. Here, we are selecting just one of the examples with the name Blink. It turns the LED on and off with some time delay. 3.6 Select the Arduino board. Step 6 To avoid any error while uploading your program to the board, you must select the correct Arduino board name, which matches with the board connected to your computer. 3-3 Unit 3 (C1TK3): Program Compilation and Uploading.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Go to Tools → Board and select your board. 3.7 Select the serial port. Step 7 Select the serial device of the Arduino board. Go to Tools → Serial Port menu. This is likely to be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find out, you can disconnect your Arduino board and re-open the menu, the entry that disappears should be of the Arduino board. Reconnect the board and select that serial port. 3.8 Upload the program to the board. Step 8 3-4 Unit 3 (C1TK3): Program Compilation and Uploading.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Before explaining how we can upload our program to the board, we must demonstrate the function of each symbol appearing in the Arduino IDE toolbar. A − Used to check if there is any compilation error. B − Used to upload a program to the Arduino board. C − Shortcut used to create a new sketch. D − Used to directly open one of the example sketch. E − Used to save your sketch. F − Serial monitor used to receive serial data from the board and send the serial data to the board. Now, simply click the "Upload" button in the environment. Wait a few seconds; you will see the RX and TX LEDs on the board, flashing. If the upload is successful, the message "Done uploading" will appear in the status bar. 3-5 Unit 3 (C1TK3): Program Compilation and Uploading.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Unit 4 (C1TK4): Common Programming Errors 4.1 Common Programing Errors The most common programing errors made when coding are: Missing semicolons at the end of lines Missing/wrong type of brackets Spelling errors Example: The simple Blink.ino sketch included with the Arduino IDE. This code will not compile, and there are 5 reasons why. Let's go over them: Line 2: Missing semicolon. Line 5: Missing function brackets. Line 7: Wrong type of brackets. Line 8: DigitalWrite function spelled incorrectly. Line 10: Missing closing curly brace. 4-1 Unit 4 (C1TK4): Common Programming Error.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Here's what that code should look like: Note: Each one of these errors, though minor, will stop the program from working. A good tip to getting used to Arduino programming is to have another program open which you can refer to, as in most cases the syntax and formatting is the same between different programs. 4.2 Serial Nonsense The serial monitor is the Arduino's console. It is where you can send any data taken from the Arduino's pins and display it as friendly to read text. 4-2 Unit 4 (C1TK4): Common Programming Error.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 When initiating the Serial monitor in code, you also set its baud rate. This number simply refers to the number of bits per second that are sent to the serial monitor. In the below example, the baud rate is set to 9,600 in code. Make sure you set it to the same value using the drop down menu at the bottom of the serial monitor too, and everything should display properly. You may notice in the serial monitor that there are several speeds to choose from. There is rarely any need to change the baud rate, unless you are transferring large chunks of data. At 9,600, the serial monitor can print close to 1,000 characters per second. If you can read that fast, congratulations, you are clearly a wizard. 4.3 Missing Libraries. Libraries written by experienced coders and released for free make it possible to use complex components such as individually addressable LED strips and weather sensors without needing to know complex coding. You can install libraries straight from the IDE by selecting Sketch > Include Library > Manage Libraries to bring up the library browser. 4-3 Unit 4 (C1TK4): Common Programming Error.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Once you have installed your libraries then you can use them in any project, and many come with example projects of their own. There are two possible pitfalls here. Using code that requires a library you do not have. Trying to use parts of a library that you have not included in your project. In the first instance, if you find a piece of code that seems perfect for your project, only to find it refuses to compile once you have it in your IDE, check that it doesn't include a library that you have yet to install. You can check this by looking at the #include at the top of the code. If it includes something you haven't installed yet it isn't going to work! In the second case you have the opposite problem. If you are using functions from a library you have installed onto your computer and the code refuses to compile, it may be that you forgot to include the library in the sketch you are currently working on. For example, if you wanted to make use of the fantastic Fastled library with your Neopixel LED strips, you would need to add #include "FastLED.h" at the start of your code to let it know to look for the library 4-4 Unit 4 (C1TK4): Common Programming Error.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices Figure 5-1 This unit covers the Arduino pins that can sense digital and analog inputs. Digital input pins sense the presence and absence of voltage on a pin. Analog input pins measure a range of voltages on a pin. The Arduino function to detect digital input is digital Read and it tells the sketch if a voltage on a pin is HIGH (5 volts) or LOW (0 volts). The Arduino function to configure a pin for reading input is pin Mode (pin, INPUT). On a typical board, there are 14 digital pins (numbered 0 to 13) as shown at the top of Figure 5-1. Pins 0 and 1 (marked RX and TX) are used for the USB serial connection and should be avoided for other uses. If you need more digital pins on a standard board, you can use the analog pins as digital pins (analog pins 0 through 5 can be used as digital pins 14 through 19). Most boards have an LED connected to pin 13, and some use this as an output indicator. Digital input sometimes use external resistors to provide the voltage that is sensed by digital Read. These resistors are called pull-up resistors (so named because the voltage is "pulled up" to the 5V line that the resistor is connected to) or pull-down resistors (the voltage is "pulled down" to 0 volts). A10K ohms resistor is commonly used value, anything between4.7K and 20K or more will work. Unlike a digital value, which is only on or off, analog values are continuously variable. The volume setting of a device is a good example; it is not just on or off, but 5-1 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 it can have a range of values in between. Many sensors provide information by varying the voltage to correspond to the sensor measurement. Arduino code uses a function called analogRead to get a value proportional to the voltage it sees on one of its analog pins. The value will be 0 if there are 0 volts on the pin and 1,023 for 5 volts. The value in between will be proportional to the voltage on the pin, so 2.5 volts (half of 5 volts) will result in a value of roughly 511 (half of 1,023). You can see the six analog input pins (marked 0 to 5) at the bottom of Figure 5-1 (these pins can also be used as digital pins 14 to 19 if they are not needed for analog). Some of the analog recipes use a potentiometer (pot for short, also called a variable resistor) to vary the voltage on a pin. When choosing a potentiometer, a value of 10K is the best option for connecting to analog pins. 5.1 Using a Switch If we want our sketch to respond to the closing of an electrical contact; for example, a pushbutton or a switch or an external device that makes an electrical connection. Use digitalRead to determine the state of a switch connected to an Arduino digital pin set as input. The following code lights an LED when a switch is pressed (Figure 5-2 shows how it should be wired up). Standard Arduino boards have a built-in LED connected to pin 13. The setup function configures the LED pin as OUTPUT and the switch pin as INPUT. 5-2 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Figure 5-2 The digitalRead function monitors the voltage on the input pin (inputPin), and it returns a value of HIGH if the voltage is high (5 volts) and LOW if the voltage is low (0 volts). Actually, any voltage that is greater than 2.5 volts (half of the voltage powering the chip) is considered HIGH and less than this is treated as LOW. If the pin is left unconnected (known as floating) the value returned from digitalRead is indeterminate (it may be HIGH or LOW, and it cannot be reliably used). The resistor shown in Figure 5-2 ensures that the voltage on the pin will be low when the switch is not pressed, because the resistor "pulls down" the voltage to ground. When the switch is pushed, a connection is made between the pin and +5 volts, so the value on the pin interpreted by digital Do not connect a digital or analog pin to a voltage higher than 5 volts (or 3.3 volts on a 3.3V board). This can damage the pin and possibly destroy the entire chip. Do not wire the switch so that it shorts the 5 volts to ground (without a resistor), this may not damage the Arduino chip, but it is not good for the power supply. In this example, the value from digitalRead is stored in the variable val. This will be HIGH if the button is pressed, LOW otherwise. The switch used in this example makes electrical contact when pressed and breaks contact when not pressed. These switches are called Normally Open (NO). The other kind of momentary switch is called Normally Closed (NC). The output pin connected to the LED is turned on when you set val to HIGH, illuminating the LED. Although Arduino sets all digital pins as inputs by default, it is a 5-3 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 good practice to set this explicitly in your sketch to remind yourself about the pins you are using. You may see similar code that uses true instead of HIGH; these can be used interchangeably (they are also sometimes represented as 1). Likewise, false is the same as LOW and 0. Use the form that best expresses the meaning of the logic in your application. Almost any switch can be used, although the ones called momentary tactile switches are popular because they are inexpensive and can plug directly into a breadboard. Here is another way to implement the logic in the preceding sketch: This doesn't store the button state into a variable. Instead, it sets the LED on or off directly from the value obtained from digitalRead. It is a handy shortcut, but if you find it overly terse, there is no practical difference in performance, so pick whichever form you find easier to understand. The pull-up code is similar to the pull-down version, but the logic is reversed: the value on the pin goes LOW when the button is pressed (see Figure 5-3 for a schematic diagram of this). It may help to think of this as pressing the switch DOWN, causing the output to go LOW: 5-4 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Figure 5-3. Switch connected using pull-up resistor 5.2 Using a Switch without External Resistors Digital inputs must have a resistor to hold the pin to a known value when the switch is not pressed. Arduino has internal pull-up resistors that can be enabled by writing a HIGH value to a pin that is in INPUT mode. The switch is wired as shown in Figure 5-4. This is almost exactly the same as Figure 5-3, but without an external resistor. Figure 5-4. Switch wired for use with internal pull-up resistor 5-5 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 The switch is only connected between pin 2 and Gnd. Gnd is short for ground and is at 0 volts by definition: There are a few Gnd pins on an Arduino board. Enable internal pull-up resistors by writing a HIGH value to a pin in input mode. Using digitalWrite(pin, HIGH) on a pin in input mode and can turn the pull-up off by writing a LOW value to the pin. When switching the pin mode back and forth between input and output, the state of the pin will remain HIGH or LOW when you change modes. If it is set an output pin HIGH and then change to input mode, the pull-up will be on, and reading the pin will produce a HIGH. If you set the pin LOW in output mode with digitalWrite(pin, LOW) and then change to input mode with pin Mode(pin, INPUT), the pull-up will be off. If you turn a pull-up on, changing to output mode will set the pin HIGH, which could, for example, unintentionally light an LED connected to it. The internal pull-up resistors are 20K ohms. This is suitable for most applications, but some devices may require lower-value resistors—see the data sheet for external devices you want to connect to Arduino to see if the internal pull-ups are suitable or not. 5-6 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 5.3 Reliably Detecting the Closing of a Switch To avoid false readings due to contact bounce (contact bounce produces spurious signals at the moment the switch contacts close or open). The process of eliminating spurious readings is called debouncing. The debounce function is called (used) with the pin number of the switch you want to 5-7 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 debounce; the function returns true if the switch is pressed and stable. It returns false if it is not pressed or not yet stable. The debounce method checks to see if it gets the same reading from the switch after a delay that needs to be long enough for the switch contacts to stop bouncing. Longer intervals ere required for "bouncier" switches (some switches can require as much as 50 ms or more). The function works by repeatedly checking the state of the switch for as many milliseconds as defined in the debounce time. If the switch remains stable for this time, the state of the switch will be returned (true if pressed and false if not). If the switch state changes within the debounce period, the counter is reset so that the checks start over until the switch state does not change within the debounce time. When using pull-up resistors instead of pull-down resistors, inverting the value returned from the debounce function is needed, because the state goes LOW when the switch is pressed using pull-ups, but the function should return true (true is the same as HIGH) when the switch is pressed. The debounce code using pullups is as follows; only the last four lines (highlighted) are changed from the previous version: 5-8 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 For testing, we can add a count variable to display the number of presses to see whether it increments once per press. Increase the value of debounceDelay until the count keeps step with the presses. The following fragment prints the value of count when used with the debounce function shown earlier: This debounce() function will work for any number of switches, but ensure that the pins used are in input mode .A potential disadvantage of this method for some applications is that from the time the debounce function is called, everything waits until the switch is stable. 5-9 Unit 5 (C1TK5): Working Principles, Features and Applications of Input/output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Unit 6 (C1TK6): Requirement of Input and Output Devices The pins on the Arduino board can be configured as either inputs or outputs. It is important to note that a majority of Arduino analog pins, may be configured, and used, in exactly the same manner as digital pins. Refer to the figure above, notice pin 0 to 13, these are the 14 digital pins that can be configured for input and output applications. Out of the 14 digital pin, 6 of them are PWM (Pulse-width modulation) enabled. The concept of input and output applications as well as PWM shall be discussed in the following sections 6.1 Pins Configured as INPUT Arduino pins are by default configured as inputs, so they do not need to be explicitly declared as inputs with pinMode() when you are using them as inputs. Pins configured this way are said to be in a high-impedance state. This means that it takes very little current to switch the input pin from one state to another (low to high or vice versa). This makes the pins useful for such tasks as picking up a signal from a digital source. The illustration below shows how the digital pins can be configure to detect water level in a water tank. 6-1 Unit 6 (C1TK6): Requirement of Input and output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Based on the set up shown in the figure, the respective digital pin, 2, 3 and 4 when configured as input will be able to detect the signal generated from the controller's 5Vdc output as they are in the water. Thus by programming the controller to read off the respective pins, digitalRead (pin), we will be able to detect the water lever. 6.1 Pull-up & Pull-down Resistors Pins configured as pinMode(pin, INPUT) with nothing connected to them, or with wires connected to them that are not connected to other circuits, report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin. Pull-up resistors are often useful to steer an input pin to a known state if no input is present. This can be done by adding a pull-up resistor (to +5V), or a pull-down resistor (resistor to ground) on the input. A 10KΩ resistor is a good value for a pullup or pull-down resistor. The 10KΩ resistor in the figure act as a pull down resistor, setting the input pin as 'low' when the switch is not activated, prevent the input pin from picking up electrical noise from the environment. 6-2 Unit 6 (C1TK6): Requirement of Input and output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 6.2 Using Built-in Pull-up Resistor with Pins Configured as Input There are 20,000Ω pull-up resistors built into the Atmega chip that can be accessed from software. These built-in pull-up resistors are accessed by setting the pin Mode() as INPUT_PULLUP. Example pinMode(3,INPUT) ; // set pin to input without using built in pull up resistor pinMode(5,INPUT_PULLUP) ; // set pin to input using built in pull up resistor When the input pin '8' is configured with pin Mode (8, INPUT_PULLUP) or pin Mode (8, INPUT), the input pin is set to input with and without built in pull up resistor respectively. 6.3 Pins Configured as OUTPUT Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means that they can provide a substantial amount of current to other circuits. At mega pins can source (provide positive current) or sink (provide negative current) up to 40 mA (milliamps) of current to other devices/circuits. This is enough current to brightly light up an LED (do not forget the series resistor), or run many sensors but may not be enough to run relays, solenoids, or motors. 6-3 Unit 6 (C1TK6): Requirement of Input and output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Attempting to run high current devices from the output pins, can damage or destroy the output transistors in the pin, or damage the entire Atmega chip. Often, this results in a "dead" pin in the microcontroller but the remaining chips still function adequately. For this reason, it is a good idea to connect the OUTPUT pins to other devices through 470Ω or 1k resistors, unless maximum current drawn from the pins is required for a particular application. 6.4 Pins Configured as OUTPUT (PWM) Pulse-width modulation (PWM) can be implemented on the Arduino in several ways. In the section below, PWM techniques, as well as how to use the PWM registers directly for more control over the duty cycle and frequency shall be discussed. The Arduino's programming language makes PWM easy to use; simply call analogWrite(pin, dutyCycle), where dutyCycle is a value from 0 to 255, and pin is one of the PWM pins (3, 5, 6, 9, 10, or 11). The analogWrite function provides a simple interface to the hardware PWM. Uses of PWM include Dimming an LED Providing an analog output Generating audio signals. Providing variable speed control for motors. Generating a modulated signal. 6-4 Unit 6 (C1TK6): Requirement of Input and output Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Unit 7 (C1TK7): Assigning the Pin Mode for IO Pins pinMode() Function The pinMode() function is used to configure a specific pin to behave either as an input or an output. It is possible to enable the internal pull-up resistors with the mode INPUT_PULLUP. pinMode() Function Syntax Void setup () pin − the number of the pin whose mode you wish to set mode − INPUT, OUTPUT, or INPUT_PULLUP. Example int button = 5 ; // button connected to pin 5 int LED = 6; // LED connected to pin 6 void setup () void setup () } 7-1 Unit 7 (C1TK7): Assigning the Pin Mode for IO Pins.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 7.1 digitalWrite() Function The digitalWrite() function is used to write a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor. If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim. Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor. digitalWrite() Function Syntax Void loop() pin − the number of the pin whose mode you wish to set value − HIGH, or LOW. Example int LED = 6; // LED connected to pin 6 void setup () void setup () { digitalWrite(LED,HIGH); // turn on led delay(500); // delay for 500 ms digitalWrite(LED,LOW); // turn off led delay(500); // delay for 500 ms 7.2 analogRead() Function Arduino is able to detect whether there is a voltage applied to one of its pins and report it through the analogRead() function. There is a difference between an on/off sensor (which detects the presence of an object) and an analog sensor, whose value continuously changes. In order to read this type of sensor, we need a different type of pin. In the lower-right part of the Arduino board, you will see six pins marked "Analog In". These special pins not only tell whether there is a voltage applied to them, but also 7-2 Unit 7 (C1TK7): Assigning the Pin Mode for IO Pins.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 its value. By using the analogRead() function, we can read the voltage applied to one of the pins. This function returns a number between 0 and 1023, which represents voltages between 0 and 5 volts. For example, if there is a voltage of 2.5 V applied to pin number 0, analogRead(0) returns 512. analogRead() function Syntax analogRead(pin); pin − the number of the analog input pin to read from (0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega) Example int analogPin = 3;//potentiometer wiper (middle terminal) // connected to analog pin 3 int val = 0; // variable to store the value read void setup() void loop() 7-3 Unit 7 (C1TK7): Assigning the Pin Mode for IO Pins.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Competence 2: Interface Microcontroller with Input/output devices Unit 8 (C2TK1): Types of Input Devices Arduino board can act as a stand-alone system it has capabilities to take inputs process the input and then generate a corresponding output. It is through these inputs and outputs that the arduino as a system can communicate with the environment. The arduino boards can communicate with other devices using digital input/output analog input/output standard communication ports like USART, IIC, and USB etc. Discrete digital I/O provides a single bit of data (on or off). Analog signals, representing a continuous variable range, such as temperature or pressure, can also be inputs and outputs for microcontrollers. Types of Input Devices Switches Resistors Voltage Regulators Sensors 8.1 Switches Connecting Switch Pushbuttons or switches connect two open terminals in a circuit. This example turns on the LED on pin 2 when you press the pushbutton switch connected to pin 8. 8-1 Unit 8 (C1TK8): Type of Input Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 8.2 Resistors Pull-down Resistor Pull-down resistors are used in electronic logic circuits to ensure that inputs to Arduino settle at expected logic levels if external devices are disconnected or are at high-impedance. As nothing is connected to an input pin, it does not mean that it is a logical zero. Pull down resistors are connected between the ground and the appropriate pin on the device. An example of a pull-down resistor in a digital circuit is shown in the following figure. A pushbutton switch is connected between the supply voltage and a microcontroller pin. In such a circuit, when the switch is closed, the micro-controller input is at a logical high value, but when the switch is open, the pull-down resistor pulls the input voltage down to the ground (logical zero value), preventing an undefined state at the input. 8-2 Unit 8 (C1TK8): Type of Input Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 The pull-down resistor must have a larger resistance than the impedance of the logic circuit, or else it might pull the voltage down too much and the input voltage at the pin would remain at a constant logical low value, regardless of the switch position. 8.3 Voltage Regulators A potentiometer (or pot) is a simple electro-mechanical transducer. It converts rotary or linear motion from the input operator into a change of resistance. This change is (or can be) used to control anything from the volume of a hi-fi system to the direction of a huge container ship. The pot as we know it was originally known as a rheostat (essentially a variable wirewound resistor). The variety of available pots is now quite astonishing, and it can be very difficult for the beginner (in particular) to work out which type is suitable for a given task. A few different pot types, which can all be used for the same task makes the job harder. The image on the left shows the standard schematic symbol of a pot. The image on the right is the potentiometer. 8-3 Unit 8 (C1TK8): Type of Input Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 The potentiometer can be used as a voltage divider to obtain a manually adjustable output voltage at the slider (wiper) from a fixed input voltage applied across the two ends of the potentiometer. This is their most common use. The voltage across RL can be calculated by: If RL is large compared to the other resistances (like the input to an operational amplifier), the output voltage can be approximated by the simpler equation: V L = R 2 R 1 + R 2 ⋅ V s . }= \over R_+R_}\cdot V_.} (Dividing throughout by RL and cancelling terms with RL as denominator) Example Vs = 10 voltsR L = 100 k Ω . }=100\ \mathrm .} , R1 = 1 KΩ, R2 = 2 KΩ and RL = 100kΩ Since the load resistance is large compared to the other resistances, the output voltage VL will be approximately: Due to the load resistance, however, it will actually be slightly lower: ≈ 6.623 V. One of the advantages of the potential divider compared to a variable resistor in series with the source is that, while variable resistors have a maximum resistance where some current will always flow, dividers are able to vary the output voltage from maximum (VS) to ground (zero volts) as the wiper moves from one end of the 8-4 Unit 8 (C1TK8): Type of Input Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 potentiometer to the other. There is, however, always a small amount of contact resistance. 8.4 Sensors In this section, we will discuss the following sensors and learn how to interface our Arduino board with different sensors. Pressure sensor Temperature sensor (LM35) Ultrasonic sensor Vibration sensor Force Sensor This is a force sensitive resistor with a round, 0.5" diameter, sensing area. This FSR will vary its resistance depending on how much pressure is being applied to the sensing area. The harder the force, the lower the resistance. When no pressure is being applied to the FSR its resistance will be larger than 1MΩ. This FSR can sense applied force anywhere in the range of 100g-10kg. Two pins extend from the bottom of the sensor with 0.1" pitch making it bread board friendly. There is a peel-and-stick rubber backing on the other side of the sensing area to mount the FSR. These sensors are simple to set up and great for sensing pressure, but they aren't incredibly accurate. Note: As it states in the Integration Guide, do NOT solder directly to the exposed silver traces. With flexible substrates, the solder joint will not hold and the substrate can easily melt and distort during the soldering. Use a male or female clincher connector instead. 8-5 Unit 8 (C1TK8): Type of Input Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 Temperature Sensor The Temperature Sensor LM35 series are precision integrated-circuit temperature devices with an output voltage linearly proportional to the Centigrade temperature. The LM35 device has an advantage over linear temperature sensors calibrated in Kelvin, as the user is not required to subtract a large constant voltage from the output to obtain convenient Centigrade scaling. The LM35 device does not require any external calibration or trimming to provide typical accuracies of ±¼°C at room temperature and ±¾°C over a full −55°C to 150°C temperature range. Technical Specifications Calibrated directly in Celsius (Centigrade) Linear + 10-mV/°C scale factor 0.5°C ensured accuracy (at 25°C) Rated for full −55°C to 150°C range Suitable for remote applications Temperature Sensor LM35 has three terminals: Connect the +Vs to +5v on your Arduino board. Connect Vout to Analog0 or A0 on Arduino board. Connect GND with GND on Arduino. The Analog to Digital Converter (ADC) converts analog values into a digital approximation based on the formula ADC Value = sample * 1024 / reference voltage (+5v). So with a +5 volt reference, the digital approximation will be equal to input voltage * 205. Ultrasonic Sensor The HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an object just like the bats do. It offers excellent non-contact range detection with high 8-6 Unit 8 (C1TK8): Type of Input Devices.
[Audio] School of Engineering IoT for Engineering Module Code: EG33004FP Ver 1.1 accuracy and stable readings in an easy-to-use package from 2 cm to 400 cm or 1" to 13 feet. The operation is not affected by sunlight or black material, although acoustically, soft materials like cloth can be difficult to detect. It comes complete with ultrasonic transmitter and receiver module. Technical Specifications Power Supply − +5V DC Quiescent Current − <2mA Working Current − 15mA Effectual Angle − <15° Ranging Distance − 2cm – 400 cm/1″ – 13ft Resolution − 0.3 cm Measuring Angle − 30 degree The Ultrasonic sensor has four terminals. Connect the +5V pin to +5v on your Arduino board. Connect Trigger to digital pin 7 on your Arduino board. Connect Echo to digital pin 6 on your Arduino board. Connect GND with GND on Arduino. 8-7 Unit 8 (C1TK8): Type of Input Devices.