Wednesday 8 July 2020

You can learn Arduino in 15 minutes

arduino z' i'm gonna talk about what they are what they can do and the basics of how to set one up to do simple things like make an LED dimming circuit control the speed of motors and even make a simple oscilloscope so what is an arduino i'm sure you've heard about these things a million times by now and if you're confused do you have every right to be because the word arduino refers to so many different things keep watching and i'll explain all of them microcontrollers are integrated circuits that are basically tiny computers they can run small simple software programs they're low powered enough that they can be powered by a battery for days but they're fast enough to process data much faster than any human being can think our do we know is a company in Italy that designs and sells circuit boards that make microcontrollers easy to use they call these circuit boards arduinoz' and there are a lot of different types of arduino z' for example you've got simple arduino boards like the arduino uno which is cheap and good enough for most projects you could use an Arduino Uno to control motors lighting cameras or even build a simple robot and then you have more fancy Arduino s with more powerful processors which have Wi-Fi Ethernet and more the company Arduino open sources all of their hardware designs which means that you don't just have to buy from them ,there are countless third-party companies that make their own variants of the Arduino hardware designs they can't call them arduino z' but functionally they're the same thing there are also these things called Arduino shields which are basically circuit boards that plug into your main Arduino circuit board and let you do more stuff for example Adafruit makes shields that let you control motors and servos without having to design motor control circuitry and Sparkfun have shields that let you turn your Arduino into a simple cell phone or an mp3 player ok so that was a quick overview of Arduino the company and Arduino hardware but there's more there's also the Arduino software development environment and this is what makes Arduino good for beginners historically if you wanted to program microcontrollers you'd have to type out a lot of binary and memorize a lot of hard to remember registers and instructions then you'd have to use special programming hardware with custom-made cables to upload your program onto your microcontroller Arduino the company got rid of all of that they created software that works on Windows Mac and Linux which makes uploading your code as simple as connecting a USB cable and clicking a button they created a programming language that lets you configure all of the Arduino Hardware products in this same way and although it's not as simple as learning Python the Arduino software is one of the easiest programming experiences you'll ever have ok so hopefully that clarifies what Arduino means in different contexts now when people say they used an Arduino for their project they're probably talking about the Arduino Uno it's highly unlikely that you'll outgrow on anytime soon so it's the perfect Arduino hardware for this beginners tutorial you can get them from Amazon cheaply and if you have the money I recommend buying one of the many Uno kits out there where you get a lot of different pieces of hardware to play with ok so you've got your Arduino Uno the first thing I want to do is get you familiar with the hardware you'll often hear Arduino skiing called microcontrollers and that's technically incorrect Arduino Tsar circuit boards that have microcontroller chips on them but they also have a lot of other stuff on there too let's take a quick tour this is the microcontroller Arduino Uno is use a series of microcontrollers called atmega AV ours they're made by a company called Atmel you can buy just the bare chips on their own for about a dollar but then you'd have to do a lot of soldering connected to the microcontroller is a crystal resonator this controls how fast the microcontroller is running I have a separate tutorial on resonators and oscillators which you might find useful now in order to upload the software you create to the main microcontroller there's actually another microcontroller this chip is what lets you connect your USB cable to the Arduino board and communicate via USB it lets you upload your programs onto the main microcontroller and once you have your program running this chip is what allows you to send messages back and forth between your computer and your Arduino and this is extremely important for debugging one great thing about Arduino is is that you can power them purely from your USB cable but if you don't want your project always attached to a computer you can just use an external nine volt DC power source with a barrel jack over here the uno has a built-in voltage regulator that will reduce the voltage to five volts and if you ever want to reboo your Arduino program you've got a reset button if you have time it's worth studying the Arduino Uno schematic don't worry if it looks complicated you don't need to understand most of it I'm just trying to get you to understand what the Arduino designers did they took an off-the-shelf microcontroller that requires a lot of extra parts to get working and they put all of those things together in an easy-to-use way and if you choose to learn more about electronics you can create circuit boards like this too okay so that's what's under the hood next let's talk about the pin connectors over here you have the power pins if you want you can connect wires here to other circuitry with 5 volts or 3.3 volts just make sure that whatever you power doesn't draw more than a few milliamps a few LEDs is okay but a large motor would be an Arduino no TX and rx is for sending and receiving serial data you could use this port to send and receive data from a GPS module Bluetooth modules Wi-Fi modules

and more pins 2 to 13 R for digital inputs and outputs normally when people think of binary you think of just spitting out zeros and ones but with most microcontroller pins you can actually activate a third state and use them as a digital input so you can use this pin as an output where it spits out 5 volts for a digital one or zero volts for a digital zero or you can internally configure the Arduino to expect a voltage on the pin and that voltage could be interpreted as a 1 or a 0 it's a system called tri-state logic and it's worth reading up on but Arduino makes it really easy to change between inputs and outputs in software here we have 6 analog input pins and these are used to measure continuous voltages anywhere from 0 volts to 5 volts I'll talk more about this later in the video finally some of the pins have a tilde in front of them and this means that you can use them to output pulse width modulated square waves which I'll also show later in the video okay enough about the hardware most of the work you do with your Arduino will actually be in the software so start out by downloading the Arduino software from Arduino CC during the installation you can expect a lot of prompts like this to show up where you're asked if you want to install various drivers just say yes to

everything once you're done connect the Arduino to your computer with a USB cable next start up the Arduino development environment the first thing that you want to do is make sure that the software tries to connect to the right type of Arduino go to tools boards and we're going to be using an Arduino Uno next we have to make sure that the Arduino environment can actually connect and communicate with your Arduino go to tools ports and select the comm or serial port where your Arduino is connected if there isn't an Arduino listed here you may have a problem with your drivers or you can try plugging your Arduino into a different USB port the Arduino software has a ton of fantastic easy-to-follow demo programs and studying these is the best place to start learning how to code Arduino calls them sketches which is just a fancy name for a program that you upload to your Arduino let's start with an example where we measure voltages that are being fed into the Arduino go to file examples basics and log read serial all the Arduino examples contain instructions on how to set up the hardware this one is telling us attach the center pin of a potentiometer to pin a zero and the outside pins to +5 volts and ground 


ok let's do that a 1k or a 10k potentiometer will work great for this we're going to be using it as a voltage divider to create a voltage anywhere from 0 to 5 volts make sure you've watched my voltage divider tutorial if you don't understand how that works every Arduino program will have two main areas the set up and the loop the set up area is where you configure your Arduino to do certain things for the duration of your program it could be information on which pins are inputs which are outputs but in this case we're telling it to send serial data to our computer at 9600 bits per second which is a pretty standard data rate for these situations the loop is the area of code that repeats over and over again for as long as the Arduino has power to it this line will measure the voltage on pin a zero and store the value of it in a temporary place in memory that we're calling sensor value sensor value is an int or integer meaning it's a whole number once we've copied the analog voltage reading to sensor value the command serial dot print line will transmit that value from Arduino through the USB cable to our computer and we can view it click upload it'll compile the sketch and upload it to your Arduino now hit the reset button now go to tools serial monitor you should see a bunch of numbers flying by as you vary the potentiometer from 0 to 5 volts you should see a number on screen from 0 to 1023 this is because internally the Arduino represents the analog voltages it measures with a 10 bit number which can be from 0 to 1023 the number scales linearly with the voltage so 2.5 volts would be halfway down at 512 you can use these formulas to convert from analog tic values to real-world voltages and vice-versa now close the serial monitor and go to tools serial plotter now you can get a graph of the voltage your potentiometer is creating over time it's basically a very simple low bandwidth oscilloscope you let's try another experiment where we build a simple led dimmer go to file examples and log and log in out serial the instructions for the potentiometer are the same as before it also says to connect an LED from digital pin 9 to ground note that it is implied that you will put a resistor in series with the LED the pins on an Arduino can't supply much current any more than about 20 milliamps and you risk permanently destroying that pin I have detailed tutorials on LEDs and resistor values but for now let's just use a 1k resistor attached to ground the other side goes to the cathode of an LED and the LEDs anode goes to pin 9 we've already talked about what an int is but this constant means that the label analog out pin will always translate to the number 9 throughout the rest of the program for example here saying analog write 9 , output value wouldn't be very clear like what the hell is the 9 for instead we're declaring analog out pin to be a place in the Arduino zs—memory that always stores the fixed constant number 9 so when we write other pieces of code we don't have to memorize which pin numbers we're using for different things it's not a big deal right now but this becomes more important when we have programs that use a lot of different pins sensor value and output value are non constant integer numbers which means they can change value during the operation of the program the term for this in programming is a variable on the arduino platform an int variable can hold a number anywhere between minus 32,768 and plus 32,767 the setup is the same as before and once again we're using the analog read function to measure the voltage on a certain pin which turns out to be pin a 0 we store that value in sensor value now we know that the Arduino internally represents analog voltages with numbers from 0 to 1023 but the analog write function only accepts values from 0 to 255 so we use the map function here to linearly scale sensor value down - values between 0 and 255 then the analog right function takes the number

stored in output value and uses it tocontrol the voltage on analog output pin which is in fact pin number 9 the serial printing stuff is similar to what we had before and at the end of each loop the delay function will make the Arduino pause the execution of all code for a specific number of milliseconds once again we click upload hit reset on the Arduino and now we have a system that can control the brightness of an LED with a potentiometer now you're probably thinking so what you can do that without the Arduino well let's probe the output of pin 9 with an oscilloscope to see what's really happening here we've got a pulse width modulated square wave we can control it with a potentiometer and it's very precise now instead of an LED let's control something else let's take this part of the circuit from my classic pulse width modulation tutorial and now we'll be able to control a motor digitally instead of a sometimes glitchy 5-5-5 circuit so you might be thinking that it's weird that an Arduino function called analog right is in fact controlling a pulse width modulated square wave well the idea is that you would use an RC low-pass filter to convert that square wave into a continuous analog voltage and I have separate tutorials on how to do this once you've looked at a few examples and understand how they work you can delete pieces of code and modify existing pieces of code to create whatever you want here I've gotten rid of the potentiometer control and serial stuff and I created a loop that turns a motor off for one second turns the motor on at 50 percent speed for one second and then goes full speed for one second so you can see how by reading voltages from sensors and writing code to control motors and servos and Arduino can easily become the brains of a simple robot in fact you can get kits to build arduino based robots for as little as seventy four dollars so those were just two easy examples of what you can do with an Arduino but you can do so much more and there are tutorials all over the internet for them thank you for watching and if you found this video useful please check out my sponsor auible audible is a company owned by Amazon and basically they have a huge selection of audio books that you can buy and listen to on the way to work or when you're at the gym they've got a promotion right now where you can get a free audio book if you sign up for their free 30-day trial by visiting audible.com slash a fro tech mots once you're there you can log in with your existing Amazon account you need to give them a credit card number but you can cancel your free trial at any time and you still get to keep the free audiobook I personally like ryan holiday he's got this amazing book called trust me I'm lying confessions of a media manipulator he's got these crazy stories about how anyone can pose as a fake expert and get quoted in news articles once you pick out your free audiobook you download the audible app on your phone or tablet and you can access your audiobooks from anywhere if that sounds interesting to you click on the link in the video description section or visit audible.com slash after tech mods to start your free trial


No comments:

Post a Comment