Using digital and analog pins for reading pins and outputting signals is fine, but sometimes it's really useful to be able to see what's going on inside the microcontroller's brain. Whilst there are many ways to send and receive information to and from a microcontroller and a computer, using the serial port is by far the easiest. The ATmega328 chip has a UART port, denoted by the TX and RX pins on the board, which can send and receive serial data. Serial means the bits are sent one by one, thousands of times per second depending on what your RAID is configured as.
The serial port is connected to the USB port on the board so we can use the built-in serial functions to send data to the serial monitor in the Arduino IDE or an external serial monitor. Using the same circuit that you built before, we'll add lines at the end of the loop to print out the values of all our different sensors and outputs to the serial monitor. This will take everything that you've learned in this chapter, including global and local variables, digital and analog pin functionality, and of course, the serial monitor.
So let's take a look. I'm going to build this code back up so that we have all of our pins configured. I'm going to re-add in here, button pin is equal to 2, reconfigure pin mode, button pin. Alrighty. Now what we want to do is we're reading the value of our potentiometer. Let's take a look at the source code for this one. So you can see that there's a little bit to it. Now I'm going to show you how to actually copy code that isn't formatted correctly into the Arduino IDE in the steps to format it properly, because it's a useful skill to learn.
It's not related to serial monitoring, but again, very useful. So whilst we get this source code block fixed in the section resources, we'll take that and we'll actually just copy it straight into the Arduino IDE, and you can tell it's not formatted correctly because everything's on the same line. There's no indentation, which is not good. So whilst this code would work, it would upload, it's not nice to read at all. So what you can do is I say void setup and you want to look for the curly brackets and see void setup, curly bracket there to start and a curly bracket there to finish.
So take everything inside of that, select it all and hit tab once and it'll shift it to the left, which is good. Now there's no space between void setup and pot pin, so hit enter, give it a bit of space to make it nice and easy to read. We'll do the same with void loophole, this space there, take all of this, everything inside there and indent it once to the right. Now I'm going to take some spacing here just to split up the different sections of what we're doing. So this is our printing area, this is all of our sensory, digital read, digital write functionality.
So what is this, what's the code doing? Well let's take a look. Our code's fairly straightforward, we're only adding in a couple of new functions, a couple of new bits and pieces to use the serial functionality. So we've got LED pin, we've got our button pin, pot pin, pin definitions, our pin modes and the new bit here is serial.begin, 9600, which you might be like, what's that all about? Well serial is the built-in serial library and you can get external software serial libraries so you can use different pins which uses a huge amount of overhead resource, but we're going to be using that hardware port, the UNO only has one UART port, only one set of TX and RX pins, so that's what we're using.
If it had multiple serial ports it would be serial 1, serial 2 and so on and so forth. Serial.begin 9600 is the board rate, which is the number of bits per second, so that's quite slow, it's the slowest possible board rate. Then we've got void loop, now what we're doing is we're reading the button value, we're reading the pot value and we're filtering it down using a separate local variable for filtered pot value to divide it by 4 so we can see the raw value versus the filtered value. And then we're writing that to our LED so we've got all that functionality bundled into one.
Now to output information to the serial monitor we use serial.print. Now we can print a string and a string is a data type that uses characters, it uses the ASCII format and it actually sends each of those characters through as 1s and 0s and then puts them back together to display the physical characters in the serial monitor. And so to use strings we use the double quotation marks. So button, I'm writing the physical text here, button is a space, then we'll write the value of the button state variable and you can see that there's no quotation marks there so if we put this as a string it would literally print out the words button state, which isn't what we want, we want the variable button state to print.
Then it prints a space and so on and so forth, it does that for the pot value, the raw pot value and then the filtered pot value. And finally you can see println and that stands for print line. Now each of these lines are going to print out on the same horizontal line and then println prints all of that out and at the end sends a return character so it knows to print everything after that on a new line. Really cool.
So let's upload this to our board and see how we go. Alrighty so now let's take a look at the serial monitor. So to open the serial monitor go to the tools menu of your Arduino IDE. Make sure that you have the correct COM port selected there so COM4 is the right one for me, it could be another number for you or a device port if you're on a Mac and go serial monitor here and it will connect to your board, take a few seconds and then we'll print everything out.
So you can see that we're using no delays in this function so it's not waiting for anything so it's printing it out very, very quickly. But you can see that when I push the button it changes to zero and it's printing everything on a new line as it samples it. Button, the value of the pot is currently at zero so let's turn that up. We see we've got our raw value here which should go to 1023, yep, and our filtered value so you can see how that scaling works. So when it's at about 50%, I'll try and eyeball this off, 512, yep, close enough to 512, you can see 127 which would be a 7-bit value.
So that's some really cool stuff. And with only a few extra lines of code you can see how simple it is to use the serial interface to communicate with a computer. In this chapter you've learned how to properly structure your code using indentation, variables and semicolons, control GPIO pins both analogue and digital and send and receive information using a computer, pretty cool! So be sure to check out the next chapter on decision making and using logic.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.