Episode: 1691 Title: HPR1691: Arduino 101 Arduino IO Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr1691/hpr1691.mp3 Transcribed: 2025-10-18 07:47:12 --- This is HPR Episode 1691 entitled Arduino 101 Arduino I.O. It is hosted by Klaatu and is about 43 minutes long. The summary is, in this episode, learn how to read and write input and output from the Arduino. This episode of HBR is brought to you by an honesthost.com. Get 15% discount on all shared hosting with the offer code HBR15. That's HBR15. Better web hosting that's honest and fair at An Honesthost.com. Hi everyone, this is Klaatu and this is, I guess we'll call this episode two of of the Arduino series, my very modest Arduino series. So Arduino, very very cool, it's big buzz word, people hear it, they get excited. I hear it, I get excited. I'm like, oh, Arduino is really cool. And I used to get excited about it like a long time ago before I even knew what an Arduino was. I just knew that they were cool. Why? I don't know. Because you'd seen a project maybe at something and they said, oh yeah, that's Arduino based or maybe you've got an actual product in your home that, you know, if you investigate a little bit further, it turns out that it's got an Arduino chip somewhere in it on it, you know, it's being driven by Arduino. So you know, theoretically, that yes, there's something cool about this Arduino thing, but you're not really sure exactly what that is. So that's okay. We can start with this thing, but I'm just warning you now that the Arduino is only I think as cool as your project, as the project that you have in mind for it is cool. So in other words, you could get an Arduino and you could be very proud of it, but unless you have something to do with it, it's going to be phenomenally underwhelming. I mean, I guess that's a little bit like, well, everything really, but I mean, I was thinking of like Linux, you know, when you first heard about Linux, you're like, oh, that's really cool. It sounds amazing and I've heard it's really cool and I'm, you know, I know it's supposed to be cool, but you have no idea why it's cool. And even once you start using it, you're like, yeah, it's cool, I guess, but it's actually just really hard. And what am I doing with this thing? But then once you start developing like all these ideas, you're like, oh, I should do this with it. And you realize how cool it is. That's when it sort of realizes its own reputation. Same thing goes with Arduino. Great one to start with is the Arduino Uno. The Arduino Uno is both cheap, small, and you know, sort of basic, but at the same time, it's cheap, small, and it has a lot of features. So it's a really good one to start with. It's I think it's like $35 or something. So yeah, and it's again the size of a business card. Everything in this episode is the size of a business card. So I'm going to see breakout the Arduino here and plug it into my computer, which I have just done. And the thing that you should know about the Arduino in terms of the computing side of the Arduino is that you will want to get the Arduino IDE, the official Arduino IDE. It's probably available in your software repository. You can look for it. If it's for some reason not there, then it's probably something that you can get from Arduino.cc. That's their site, Arduino.cc. And sure enough, if you go there, you'll find a download link for the IDE. If memory serves, you can just run it just as is. I think it's sure it's Java-based. The IDE itself, I mean. So it's pretty simple to just kind of fire up. But in terms of connecting to the Arduino, that can be a little bit more complex sometimes. Depends on how you're installing this thing. But I've seen it on certainly both Mint and Fedora. You install it, and it actually hopefully prompts you. It says, hey, it looks like you're going to start using Arduino. You need to create like, I think it's a group for Arduino, or like a permission policy or something. I forget what it's verbiage exactly is, but it's telling you that you need to set something on your system so that you can actually control the Arduino, because as we all know, or will after I say this, Linux, when it sees hardware, it doesn't just let anyone control it. It wants to know who gets permission to control it. And the reason for that, obviously, is because if you've got equipment plugged into the computer, I mean, not necessarily on a multi-user system, you don't necessarily want all your users being able to access that, certainly if it's something that requires only one user to be able to access it. So there's a policy around hardware devices, and the automated features of the installer on whatever distribution you may or may not be using will solve that for you. It'll flip whatever bits need to be flipped, and then it'll prompt you to log out, you'll log out, you'll log back in, not reboot, just log out and log back in, and then everything's ready to go. It's really simple. If you're not using one of those distributions that has it all packaged up and prepared for you, such a slackware like I am, then you may need to do a little bit of configuration yourself, but it's not hard, and hearing about it will actually make you understand the automated stuff a little bit better anyway. So what I did was I plugged the Arduino in to the computer and started up the Arduino IDE, and sure enough, it had no idea that an Arduino was connected. So that was a little bit inconvenient, but it's not hard to solve at all. So what you can do is you can do an ls space-l on slash dev, slash serial s-e-r-i-a-l slash by dash id slash, and then if you just hit tab a couple of times, you'll see eventually that there's a USB dash Arduino, blah, blah, blah hooked up to your computer. So if you hit enter on that, then it will tell you what that is, the simlink to, and it's actually a simlink to a TTY device. So then if we do an ls slash dev slash TTY ACM0, that's what it's a simlink to. Sure enough, such a device exists. So ls slash dev slash TTY ACM0 is what it's called on mine, and again if you look on yours it'll probably be something very similar. And that's where the Arduino exists, and sure enough if I do it in ls dash l, well right now if I do that it comes up correctly because I've set this up already before, but pretend maybe that that wasn't the case, and you could see that it was like, I think the default permissions were maybe 600? Maybe 640 or 620? I don't know. It was definitely not, yeah I think it was actually 640, because the dial out group owns that. So anyway what we want it to be is readable and rightable by other users other than just root, and in my case if it was just completely from scratch, I would probably need it to be to anyone. So I would Chamod this thing to for instance 666, so Chamod 666 slash dev slash TTY capital A capital C capital M0. Now once I did that sure enough the IDE suddenly realized yes you've got an Arduino Uno on dev slash dev slash TTY ACM0, and I was able to use the Arduino as if though nothing had ever happened. So that's what's going on in the background if your installer did all that for you, and if it doesn't do that all for you you're in luck, you get to be geeky, and you get to Chamod your dev slash TTY ACM0 to yourself. To finesse that you could make a you dev rule that would listen for for instance a USB device being plugged in on the subsystem USB for a certain vendor ID which you would find out with LSUSB or product ID whichever you prefer, and when it detects that happening on the action add maybe you could say okay Chamod or yes set mode 0666. So you can write your own you dev rule do a reboot just to be sure that your rules are reloaded, and next time you plug in the Arduino it'll just all work for you. But again all that is automated for you on other distributions I'm only speaking from experience on Slackware, and that would probably stand true for like Slackware Engine 2 and I don't know maybe Arch I don't know maybe if if someone did the the build script a little bit fancier it might not might not happen on Arch. It really depends it's up to whoever does a build script for anything really on any on any system. I just did it on Slackware myself because I kind of understood what was going on. Okay that's that so that's how to get the Arduino up and running in the Arduino IDE that you have downloaded and installed by now. There's a Tools menu and the Tools menu will kind of show you where which board you have plugged in. So in this case it has auto detected that I have an Arduino Uno board. It generally does auto detect but I wouldn't at all say always. I've had devices that have an Arduino chip on them like little hobby electronic devices like little not drones but like robot things. Not for me personally they've been at the makerspace that I've all interior at so it's been a bunch of stuff lately that I've been doing where yeah it's like oh this is some product that you buy off a shelf but look at it it's got the Arduino on it and you can now reprogram it and some of those just have not been auto detected and you have to look in the documentation and see what it is and then you say oh okay this isn't Arduino like technically it's an Arduino Nano with a 18 mega 328 okay well I'll set it to that. So it just kind of depends but a lot of times with the main Arduino you know branded things it'll auto detect it. It may or may not detect your serial port and again like now you know how to find that out you you look in your dev tree but in my case I've already yeah it just kind of detected that as well because I don't know why it just did. When I very first originally started the thing up it wanted to look at dev or serial port like com 0 or something I don't know what that is but that's what it was looking at and then once I had set my permissions it switched over to what it has always been since which is ACM 0 so or TTY ACM 0 okay so there you go. So let's let's plug this Arduino in it's plugged in let's power up our breadboard so again we're going to take a red cable maybe from the 3.3 volt pinhole on the Arduino to the red side the red column of my breadboard and a black cable possibly or blue if you if you don't have black from the blue strip the blue column of your breadboard over to the ground pin on your Arduino so now we've got a circuit got a circuit going on in our breadboard. So what we're going to do here is I've got and I mean I apologize obviously I can't really guarantee that you have the same components that I have so a lot of this is just going to be theoretical because that's just what we've got is is theory but I think it'll mostly make sense so I've got this little servo here that I borrowed from the makerspace and it's so a servo goes 180 degrees motors go 360 servos go 180 so this is a servo it'll go 180 either way and from the servo motor there are three cables there's a actually a brown cable in this case a sort of a red orange cable and a yellow cable and kind of deducing again the electronic sort of convention I'm figuring that the brown is negative the red is positive and then the yellow since this is not just a binary object like this isn't sort of like am I on or am I off but how far should I rotate the yellow is sort of the data cable if you think of it that way so I'm going to plug this brown cable into I'm just going to go straight into my power grid I'm not going to mess I know that in the last episode I had you connecting the grid to the you know the the middle part to the columns on the side and that was that was to show you the principle of all this but in reality we don't need to do that so we can just go straight into the the negative side so I'm going to take the brown cable and plug that into the negative column the blue column on my breadboard and the red thing I'm not going to put straight no actually the red I can I am going to do that I'm going to put that straight into my red column on my breadboard okay so the data column is the one I'm going to do something obviously different with and that's kind of important because we don't really know what to do with it yet so what we can do I know I can tell you this what we want to do with that is we want to send we want to send it signals from the Arduino so the Arduino obviously sends out digital signals so we're going to plug the data cable into the Arduino itself and again if you've got those links that I gave you in the last episode and if not maybe I'll either include them in the show notes this time or I'll include them in the or you can just go look at the previous episode but on the same sort of strip as the ground cable at the top of the Arduino Uno there's it's labeled digital and then it's got numbers so it's like 0 through 13 I think yeah so I'm going to plug this into port 13 digital port 13 that's where my data cable is now and that will matter in the future in the very near future and so that's output really so what we can do now and this is a little bit difficult because servos there's not really any way for this servo for instance to know where it is so it doesn't know if it's at 0 degrees or 90 degrees or 180 degrees right now it just knows that it's a servo and certainly the Arduino doesn't know where the servo is so what we'll do is we'll program a really simple really really simple program to just tell this thing to move the servo so the first line of our code will be hash include space servo.h and that is very specifically include or hash include space and then open open tag like a greater than sign servo with a capital H dot H I mean with a servo with a capital S and then dot H and then lowercase bracket or less than rather less than bracket you know like a HTML tag okay so that's including the library servo.h and that's just the default Arduino library it's just it's just kind of included there are other libraries that you can get for fancier equipment but for this sort of basic stuff you don't need anything extra so that's convenient so the next line will make it we'll declare a servo and to do that you just say servo with a capital S and then name it something so we'll call it my servo simple enough so now we've got a servo sort of in our code and we're saying okay we want to use this class called servo and we want to make a specific instance of this class which we're just going to call my servo because that makes sense and then what we'll do is we'll create a variable and to do that we'll use int int because it's going to be an integer so int space servo position semicolon did I put a semicolon on the it should be servo my servo semicolon and then the next line int servo position semicolon so that's all that so then we can just basically let's do void setup parent parent so that's the word void and then space set up all lowercase and then parentheses parentheses so that's the rounded brackets it's parentheses parentheses and then the next line or on the same line depending on how how you like to code one curly bracket my servo dot attach parentheses 13 parentheses semicolon and then close the curly bracket so you're you probably get from that that we've just attached like we've taken this specific instance of the servo which we're calling my servo and we're doing something with it we're attaching it to the Arduino so we're telling the Arduino hey you've got a servo located in your 13th port so 13 of course is the digital port that we plugged the servo's data cable into and then within the void setup before the the closing curly brace let's tell it to set the servo position and the way we're going to do that is with a my servo dot right that is dot WRIT and then parentheses zero close parentheses semicolon that ought to work to be honest basically yeah that should work we can try it and you can verify that it's going to work by hitting the oh you know we need a loop sorry we do need a loop so let's do a void space loop parent parent semicolon or not semicolon curly bracket curly bracket and you just leave that empty we're not actually going to use the loop right now it's just it'll just be there so now if you hit the little check check button the little tick mark in the upper left corner just under the file menu it should tell you that it's done compiling and it shouldn't really flash or blink any ugly colors at you it should just kind of tell you done compiling and then what we can do is send that over to our to our Arduino with the the right pointing arrow it's it's if you roll over it says upload and once you send it to the Arduino it should if your if your um if your servo isn't at the zero position then it will go to the zero position after after you if you've uploaded your program now it's the void setup clause in there that all that does it runs only once that's kind of the init sequence or the init um it's it's the init function um of of that servo it or of this program rather it it runs once and then it gets caught in this loop which is empty so it basically basically stops there's a reset button on the Arduino Uno anyway and if you hit that it would it would attempt to run it again although it's already at zero so functionally it's not actually doing anything but what I could do now in my IDE is tell it to write my servo dot write 90 instead of zero and then hit the upload button and let's I'll hold this up right next to the microphone so you can maybe hear it reposition itself let's hit upload and there it went um so it just moved 90 degrees in whatever direction it chose to move so we're we're doing really really simple obviously things uh and that's that's just outputting from the Arduino it's not sensing anything right and that's kind of that's where it gets a little bit more exciting is when you can actually deal with inputs and outputs so you could maybe have a loop that sat there and did nothing actively did nothing until it received some kind of input and then it did something else something new and we can do that and there are various kinds of input devices there are light sensors temperature sensors there are potentiometers there are sound sensors all kinds of things so just to keep this really really simple we're just going to use ourselves as input strangely as that strange as that may seem so I want you to take a cable and plug it into the analog input the 0th port of the analog input just to keep things consistent here so if you look on the picture on the open clip art thing at the bottom next to power there's another strip of little pin holes and it's called analog in and there's zero through five so if you plug a cable into the 0th port of the analog pin row then we've now essentially we've got an input device believe it or not and the way that we can make that work for us well that's a little bit more complex okay so let's go back up I'm going to copy all this code and put it into a new window or new sketch is what they call it don't put it into a new tab of your same of the same Arduino window because that sees it if they're tabs they see them as the same program and things will go wrong so it has to be a new window so just go to file new and then that'll open up a whole new window it'll call itself something something different like sketch underscore you know the date or whatever and and then paste in the code and now we can start modifying it so the first line we've already got include servo h that's easy second line servo my servo that's also easy third line int servo position semicolon easy okay now we need two more variables for the servo position and we'll say int int space servo max equals 180 semicolon int servo min as in minimum and maximum equals 90 semicolon so that's int servo max equals 180 semicolon int space servo min equals 90 or actually let's do zero zero semicolon there and now we'll also need something else we're going to need let's do some we'll call them values so int value semicolon and int value min or rather a max and let's set that to 600 semicolon and then int val min and let's set that to let's say I don't know 50 or 40 or something like that semicolon okay so we've got int value semicolon and that's just empty we don't need to declare a value for that int val max equals 600 semicolon int val min equals 50 semicolon those are our variables and now we're going to go into the void setup so again this thing only runs once and we'll just do a void setup well it's basically already but I'll also I'll read it to you void setup parentheses parentheses and then curly brace my servo dot attach parentheses 13 close parentheses semicolon and then close the curly brace now we go into our void loop and so notice there we're not setting the initial we're not doing a my servo dot right in this version of the program so delete that that second line from your setup so now we're going to go down into the void loop and it'll be void space loop parentheses parentheses and then curly brace and we will we will finally declare our value and the value is going this is exciting value equals analog read parentheses zero parentheses semicolon so analog read think about it we're reading data in from the analog port and which port are we reading zero because we plugged it into the zero with port remember that's why I wanted us to all do the same port um so I mean if you plugged it into five then you could say oh analog read five or whatever but we're just keeping it at zero but I just want you to understand what that number means it's not we're not setting anything we're saying I want you to read data in from the analog the zero with analog port and I want you to assign it to the value uh the the variable called value okay next line we're going to say um servo position equals and this is going to be a map so we're saying servo position equals map parentheses values comma val min comma val max comma servo max comma servo min close parentheses close uh rather uh semicolon so all that sort of does is it maps several values um uh several several variables rather um value was probably not the best name for that variable but anyway um several variables to to where we want you know to to where we want the um servo position to end up and so it's it's saying to take the value and then to take the value minimum and maximum and set them within the minimum and maximum set that to the servo uh minimum and maximum because remember we don't have you know the value minimum of our input device is not necessarily the same as the servo minimum and the value maximum is not necessarily the same as the servo maximum so we have we have to kind of distribute those values um relatively to one another and that's just something that's the map function in the Arduino language okay so then servo position we're going to set we'll type servo position equals constrain and again that's another function that we're using uh parentheses servo position comma servo min comma servo max so we're telling the Arduino that the the farthest the servo is permitted to go is the servo minimum and the servo maximum and then finally we're going to write the position of the servo to the actual physical servo and that's the what we did and the the previous little application we just did a min servo dot right and then parentheses and instead of just hard coding zero or 90 we're going to say whatever the servo position is so servo position close parentheses semicolon and then close the curly brace now if we verify that code with a little tick mark button at the top left it should verify and if not check your spelling check that you have semicolons after every line that you've closed all your parentheses all that stuff and then assuming that worked I'm going to upload it to the Arduino so I'm flashing the chip and it should tell you that it's done somewhere or at least not error out now if I take this cable and touch my finger to it you may hear that the the servo is responding to my just my touch and that's simply really just because because I'm sending it you know I'm conductive being and so I'm sending it like signals from from my body not that big of a deal it's you know pretty rudimentary little sensor not very reliable you certainly would not want to use that for anything that you actually care about because you can't really control what you're sending to it when you're just touching a cable it really just kind of all depends and it's yeah it's kind of it's it's basically ridiculous it's it's really just based on arbitrary factors but there I'm putting my multi tool onto it that's not sending different signals so yeah I mean it's just it's silly don't actually do that for anything but but it does demonstrate so now if you if you weren't running this code like let's go back to our other window just to prove that this this isn't some kind of magical thing if you run the other code it sends your servo oh I had it on 90s okay so it just sent my servo to 90 whatever that is to that servo and if I touch this analog wire nothing happens so it's it's not as if the you know that's the fact that a wire is plugged into the analog port of the Arduino means nothing but once you type in code for the Arduino to listen to the are doing to the analog port zero and upload that code to it then once once it does that then it is it's listening for input on that that port and when you send it input then it it maps those values of whatever it's getting to to its servo and I have no idea how these values are being detected from this read I really have no clue but what I do know is that if you had like a light sensor you could use the LDR if you had like a light sensor then you could for instance have a light sensor plugged into the analog port I think it would probably also need power and obviously a ground and then shine you know a torch into the light sensor and suddenly depending on how bright the the light was the the servo would rotate a certain degree and then if you pull the the little flashlight away from the light sensor then the the the servo would rotate you know as or it would return to its original position or whatever and that is what we're doing obviously so now that you know how this is working you can kind of look at that code the map code and see that we're saying okay take the servo position or set the servo position rather to a to to the mapping of the overall value of whatever we're getting at the analog read and make the assigned correspond the the minimum value to the servo max and the max value to the servo minimum so it's rotating basically inversely dependent upon what kind of signals it's receiving to from the analog signal and it's never going so far that it exceeds the servo minimum or the servo maximum which is nice because you don't really want to try to you don't want to strain the little servo really so there you go that's that's the really really the basics of Arduino the takeaway really is not necessarily how to make a servo or go or or how a buzzer buzzes it's it's that well it's how electricity works and how you can use it and also that the Arduino is it's if you think of it at least for me if you think of it as just a piece of software rather than just don't think of it as hardware because if you think of it as hardware you'll start to try to like figure out how things are connected and stuff like that and as you saw from our first program which doesn't require any input that you know that that can mislead you because if if the Arduino doesn't have code to respond to input then you can send signals to it all day long and it's not nothing's going to happen so if you think of it as software and just think okay I've got some set of inputs and I've got some set of outputs and how do I get inputs into it well I use these little hardware devices and I learn about them and I learn what their values are their potential values are and and how to wire them into the Arduino and then according to what I what I research I can import a library if necessary and then I could use like a potentiometer and the more you turn the potentiometer the more the little servo turns and so on and and using resistors you could make things go dimmer or faster or slower or whatever there's a whole world out there of sort of controlled electronics that you could you can harness with with really just an Arduino and like a breadboard and if you want something more permanent then you'll want to eventually get little circuit boards that you can actually solder your cables to and and make it you know a lot more permanent and that's a lot of fun as well but again all that requires a project and so what you'll want to do really at some point I mean definitely get an Arduino like if you're interested in one get one I think I've shown you that it doesn't take a whole lot to get started with one just you do have to you're you're gonna want to buy some components so that you can mess around with it and make it do things in the real world that you can see because otherwise it's just a circuit board that you know in theory it might do something so you'll want to get some components and then just hook them up and start programming and see what you can make occur and and see what conditions you can make things happen under you know like how you can change the response depending on on a different input or something just play around with that and then once you've got all that down once you get that theory solidly ingrained in your in your mind then you can do all the other you know you can kind of come up with some kind of really cool project and kind of figure out what you want your Arduino to actually control and and then you can start programming for it and and hooking up all the different hardware and and really see where it takes you it's it's a very exciting little world it's really cool because the the entire Arduino board itself is open source so this is a big deal it's it's very exciting and you should try it if you're interested in it I think you'll really enjoy it and it's not as scary as it might seem and it's not really as puzzling as it might seem it's it's just a little bit of understanding of how electricity works it's a little bit of understanding of how input and output works and from there it's sitting around playing with with code playing with wires and playing with low signals of electricity so have a great time I hope this has been helpful and if you come up with anything cool let me know you've been listening to hecka public radio at hecka public radio dot org we are a community podcast network that releases shows every weekday Monday through Friday today's show like all our shows was contributed by an hbr listener like yourself if you ever thought of recording a podcast then click on our contributing to find out how easy it really is hecka public radio was founded by the digital dot pound and the infonomicom computer club and it's part of the binary revolution at binrev.com if you have comments on today's show please email the host directly leave a comment on the website or record a follow-up episode yourself unless otherwise stated today's show is released on the creative comments attribution share a light 3.0 license