Okay. So next, we're going to talk about IoT platforms, how they're actually designed and how you can program them to make them do real things. So at this point in the course, you know about all these different components you can use, how you plug them together with wires, how do you actually build real IoT devices? We're going to start getting a sense of that. We're going to start by studying a particular platform known as the Arduino Platform. The Arduino platform is a great platform to learn about IoT. It is a hobbyist prototyping platform for IoT devices. It simplifies a lot of things. It's designed for novices. But don't be fooled, it's a very powerful platform under the hood. It exposes you to all the same stuff you get if you are really programming micro-controllers yourself. You could do a lot of things with it. You can plug in virtually unlimited array of sensors, and Indicators, and displays, and motors, and all that stuff. Arduino's started as a research project at the Interaction Design Institute in Italy. Its name comes from a bar in Italy where the founders used to meet. The goal of Arduino was to make IoT easy for people to do. They wanted to make it easy for novices to create devices that interact with their environment using sensors or actuators. The Arduino platform has a few key technical components. Arduino provides both hardware and software. So for the software, the software is provided to simplify development. It provides an IDE, or an interactive development environment, where you can type in your program, it also provides a collection of supporting libraries to simplify your code. You don't have to deal with all the little details of how to write voltages to outputs or things like that, the libraries hide that from you a little bit make that process easier. The code that you write will be written in C++. When we talk about Arduino programs, we call them sketches. They're typically saved with the extension.ino. The Arduino platform has a set of board designs that are provided, and they use different sorts of microprocessors and controllers, but it's a very open platform. Both the hardware and the software is open source through Creative Commons and GPL, respectively. Here is in Arduino Uno, which is a particular board made by Arduino. The board itself has a set of pins. You can see them here. These little pins are things you can stick wires into them. This is a standard interface, you can buy wires and plug them in. There's different kinds of pins. There's some pins that are digital, so you can raise or lower them, send a zero volt or a five volt out. There's also analog pins in the lower right. Those are pins where you can read analog values. There's also power pins where you can send voltages out to your board to power it, or to your other devices to give them power. There's a few other pins as well. There's a ground pin, there's a pin for communication using the I2C protocol. So Arduino can interface with those breakout boards and those ICs that we talked about before that use I2C. There's also an analog reference voltage pin. What that does is that tells the Arduino what the analog reference is. So if you're reading in analog data, you can tell the board, "Here's the maximum analog data voltage that I'm going to read in." Then it'll tell you what the voltage is between a maximum and minimum, specified by the analog reference. We'll talk about that a little bit more later. There's a few digital pins you're going to notice. They have little [inaudible] next to them. You can see the digital pins at the top are numbered 0, 1, 2, 3, 4, 5, up to 13. Some of them have little curly [inaudible] in front. Those pins are PWM pins. They're pulse-width modulation. So if I want to write digital data, I can tell the Arduino board to do that. I can tell it to raise a pin or lower a pin. If I want to read analog data, I can do that on the analog pins in the lower left. If I want to write analog data, like if I want to send to 2.7 volts or 2.6 volts, I do that using pulse width modulation, and I can only do it at those pins that I have circled there. In addition to that, there are some pins for serial communications, using the serial communications protocol. So it turns out I can communicate serially using the USB, there's a USB port on this thing. But I can also do it using those pins as well, if I want to interface with other ICs or other boards. There are little LEDs on the board to tell you information about the board status. These two LEDs I'm showing here are transmit and receive LEDs. So if I'm sending data at the I2C communications port, those will start lighting up and flashing, to let you know I'm trying to send data. There are also some ways I can supply this board with power because it uses electricity. You can get electricity through the USB port, you can connect it to a computer and power that way. There is also a DC power jack, then there's some stuff on the board to take that power and convert it. There's a voltage regulator to keep that power stable, there's a diode to prevent against reverse polarity, and there's also a little LED onlight in the upper right there. That tells you the board's getting power. So when you plug it in and that light is not on, then something's wrong. The computation of this board is done by a few components. The main computational unit is the ATmega328. This is a micro-controller and this is the main processor for the board. So this is the main component you're going to be interacting with. When you develop code and load it on the board, it's going to be loaded into the ATmega328. You can also see there's a little ATmega16 in the upper left there. The ATmega16 is not something you interface with directly, but it's necessary for the board to interface with the USB. It's used to connect to the USB, make sure boot-loader runs, and things like that. There's also an oscillator which drives timing and the program counters on these various processors. There's a "Reset" button. If something goes wrong on your board, you could push the "Reset" button, and that'll start the board over. It will start running the boot-loader again and start running your program from scratch. There's also a USB port, and there's two other serial programming ports as well. The USB port is something that you can use to interface with other devices, like your computer, for example. So one common way to program these boards is you write a program on your laptop, then you connect this thing to your laptop using the USB port. Then you push a button on your computer, it'll load the program through the USB port onto the ATmega328. The USB port can also provide power to the board, so you don't have to use a DC power jack. There also these two in circuit serial programming headers for the ATmega16 and the ATmega328. You probably won't be using those initially, but these are useful if you really want to debug the board directly. You can interface with those indirectly debug the main processor in your code running on it. You don't have to use that if you use the USB, the USB can do some of the same functions as those. That's probably enough to get you by initially. So this is the Arduino board and this is what we're going to be working with initially. We're going to be talking about this. One thing I want to show you is, I want to show you this processor in a little bit more detail. So let's talk about the ATmega328. It's a very important part of the design. So let's talk about it a little bit. What if we could zoom in and look inside that micro-controller. It's going to be helpful to understand what's inside of it, because when you start writing programs, you're going to be writing programs that interface with these different components, and access them. So it's good to know what's inside there. The main thing in there is the CPU. There's something that does computation, runs your program. There's also memory do to some little units in there, that have SRAM, and flash, and EEPROM, and that's all inside this chip. So your program can access these things directly. It can read and write to flash, and SRAM, and various things on the chip. There is also a unit associated with I/O and communication. So this thing can speak I2C and these various protocols, it does that with these logic blocks here. There is a power unit. This is for power supervision. You can power-up and power-down things, do power savings. There's timing units you can use for timers, there's debugging units, and so on. So when we get into programming, all the stuff is going to look familiar because your problems can call these various things using libraries. Just so you know, there's other boards besides the Arduino Uno, and there's different trade-offs. So when you go out and you want to buy one of these boards, you can choose what you want. We talked about the Arduino Uno. I'm listing the various statistics on it, what kind of processor it has, how fast it is, and so on, right there. There's also a version of the Arduino Uno that has [inaudible] built-in. So if you wanted something that has the ability to communicate over the network already built into it, you can use that. You can also get ex-pander boards for the Arduino that do that too if you want though. Then you can call these functions using your program to send data. Very easy to do. There's also the Arduino Nano which is a version of the Arduino that was designed to be small. If you want to use it for some small thing you're building. There's also the Arduino Mega which is a big version of Arduino. Has more pins, it's more powerful, has more memory, and so on. There's also a lot of third-party Arduino compatible boards that have been built, there's clones of the Arduino platform like the Elegoo Uno, there's the Adafruit Trinket, where they really focused on trying to make something really small, so that's if you want to make smart clothing or when you want to hide it. There's also boards that are used for drones. There's a FlyDuino, there's even boards that are designed to be launched into space like the Ardusat, and so on. So there's a lot of boards out there. So if you want to do something specific with your design, you can use these. So another thing about Arduino is they are really focused on making the design modular. So one of the things they did is they made it so you can plug other components into it easy. These components are called shields. So what I can do is I can take an Arduino and then I can buy a shield for it. I can plug it in, and the shield provides some additional functionality. If an Arduino and I want to make it have Wi-Fi access, or Bluetooth access, or something like that, I can buy a shield that has that capability, and then just slot it on top of the Arduino board, and I can stack these as well. So I could take an Arduino Uno and plug in an Xbee and then plug in SD card Storage and have a modular platform I can grow over time and there's a lot of shields out there you can buy depending on what you want to do. There are shields for cellular access, GPS, cameras, speaker, sensors, IR receivers, medical sensors and so on. So people have really built a lot of these shields to really extend the platform to do various things. So how do we program an Arduino? Well, usually when people program micro controllers, these high-level languages. You typically don't write assembly anymore, because compilers have gotten pretty good at optimizing things for micro controllers. The Arduino platform makes this process even easier because it has a simple IDE and so libraries and objects that you can use to get to hide some of the details from here. That said, if you want to you can look into libraries and you can see what it's doing. So it doesn't really hide any sort of the education from you, so it's a good platform to learn as well. Some of these Libraries are written by Arduino to make the process easier and some of these Libraries are written by Atmel, the company that made the microcontroller. So Arduino sketches are C++ programs calling extra libraries, and Arduino programming is similar to other kinds of embedded programming and the approaches that we're going to use are very generalizable to other micro-controllers and platforms. So in this lecture what I'm going to do is, I'm going to teach you how to do embedded programming. In the context of Arduino, but what we're going to learn is easily generalizable to other sorts of micro-controllers and domains and so on. So when you download the IDE for Arduino, which is one of the first things you're going to do. It looks like this, it's going to be a little window and there's going to be a space there where you type in your program and there's going to be a button where you can compile your program. You click on that. There's also a message window. So if there's a compilation error, it'll display it down there, and there's an upload button. So that's what you push when you want to actually upload your code onto your board. So to set up this whole situation, you need to do a few steps. So the first thing you want to do is you want to tell your IDE how to connect the Arduino. So what you're going to do is you're going to turn on the ID, run it and then you're going to take your arduino and take the USB cable and then plug it into your computer and then you're going to go in the IDE and you're going to do these steps I have detailed here. You're going to go to tools and choose port and then select whatever communication port you are using. This is a step that can sometimes go wrong. You might not see that communication port there, you might not know which communication port to choose. So I have some hints here you can follow if you get stuck. But once you do that, your IDE is connected to your board and then whenever you hit upload it'll load the code onto your board. Second thing you want to do is, you want to tell the IDE what sort of board is connected to it, because the IDE needs to know is this Arduino, or is this a mega or what sort of platform is this, what sort of microcontroller is this. So you tell the IDE that as well using these instructions I have here. Then what you do is you go head and you write your program. So here's an example program I have here, this is an example Arduino program. One thing you're going to notice is that there's two functions. There's a setup function and there is a loop function. So our Arduino programs are written like this. The setup function is a function that's called once when the Arduino first starts running. Then the Arduino sits in a loop and calls the loop function over and over. So you want to put your initialization stuff in the setup function, and then you want to put what gets executed over and over when your Arduino is sitting there in the loop function. So those are the two main functions exposed by Arduino. So if you know C++ you might be thinking, ''Well where's main? Where's the main function here?'' The answer is it's somewhere else, it's hidden. The Arduino libraries hide that from you, just to make it easier a little bit and they only expose these two functions for you. But the main function is there. What that main function does is it calls setup once and that sits in a for loop forever Client a loop function. There's a bunch of other functions you can call and I'm showing some examples here. One of the functions that you can call which is really useful is called pinMode, and what pinMode does is it configures a pin on the board. Remember all those pins numbered one through 13, all the analog pins and digital pins? You got to tell the Arduino how you're going to use those pins. You got to tell it whether the pin is an input or an output and if it's an input or an output, what kind of input and output it is. There's also digitalWrite, and what DigitalWrite does is it sets that pin to a certain value, so it tells Arduino to output a voltage to a certain level. There's also digital read which can breed a pinState. So here I have a little program and what that program does is it starts out in its setup, it initializes pin 13 to be an output. So what's it's doing is it's telling the Arduino controller, okay pin number 13 that's going to be an output so get ready to send values to that, whenever I tell you to do so. Then there's a loop function here and what the loop function does is it first calls digitalWrite, which sets pin 13 to be high. In other words, it tells Arduino to make pin 13 be five volts and then it calls delay which waits for two seconds and then it calls digitalWrite to make pin 13 low, so lower down to zero volts and then waits for two seconds and then it's going to run that again and again. So what you can do is you can write this program, upload it to your board, you could hook up a little LED to your board, and what do you think it's going to do? Well, when you hook up that LED what's going to happen is that pin's going to go high and low and high and low. So the LED is going to flash on for two seconds and off for two seconds over and over. So already you can imagine all sorts of powerful things you can do here. You can hook up motors, you can hook up lights in various things, you can make things actuate and go on, just with these functions alone. So this is already pretty powerful. So the steps you need to use when you kind of create an Arduino program are these. First you want to verify and compile your code. You want to make sure your code is as correct as possible and then you compile it. So there's a verification process where the Arduino compiler does static analysis on your code just like any compiler and you do that by hitting the check-mark button. So the Arduino microcontroller runs your code, but it turns out it needs a little bit more code than that. It needs to know how to interface with the USB port and read data from that and stuff like that. So you might think, well it probably has an operating system that runs on there and the operating system takes care of all this. Well micro-controllers don't typically have operating systems because they are small, don't have much memory. So what they have is something called a boot loader which is a little program which sits there and it takes care of getting the program in and starting it running and then it just goes away unless you program run. So that's the bootloader. You need to upload boot loader on the board, so the IDE takes care of that for you. So the IDE calls something on the back end which uploads bootloader onto your board for you. So you don't need to do anything yourself for that. Then the third step is you upload your code on the board and you do that by hitting the right arrow button in the IDE. Then once you do that, once your code is uploaded on the board, your bootloader will start it running. So your program is just going to start running automatically. If you have a program that makes the LED flash, you upload it and your boot loader will immediately start making that LED flash. If something goes wrong, it can be a little tricky to troubleshoot because the Arduino doesn't have, you don't plug in a computer monitor to it. You can't really run GDB, it's hard to step through and see what's going wrong if your code's not working. So there's a few ideas for how you can troubleshoot things and these are good tips for when you are troubleshooting real embedded systems. Real programmers do this too. So in the context of Arduino, make sure first of all you have the right board type and ports selected, because you have the wrong board type and those sorts of things selected may be uploading the wrong sort of code or the wrong sort of bootloader to the board. You can also check to make sure your circuit is hooked up right, if you have a short-circuit, the LED on light on your board might not go on because there might not be enough power to supply voltage to your circuit. So you check for short circuits. You can use a multimeter to check output values. A multi-meter is a little device you can get for like 10 or $20 that can sense voltages, so you can kinda plug the multimeter in the pins that you think are going on and then you can see if they're actually going on. You can also use something called a serial monitor to log progress, we'll be talking about that. It turns out that the board can print values back over USB to your computer, that's useful for debugging. You could also use a simulator or an emulator, there's open-source software visualizations of Arduino which you can run your code through first and make sure it works. Those can be easier to step through than using a real board.