Using with Arduino | Part 1

7 Просмотры
Buy access to all our courses now - For a limited time just 19USD per month with a 30 day satisfaction or your money back "No Hassle" guarantee!

***Get the code, transcript, challenges, etc for this lesson on our website***

Are you trying to use to get data from a serial port to your Arduino? Maybe you’re using the Arduino Serial Monitor window and sending in data, or maybe you’ve got a program running on your raspberryPi sending data via serial to your Arduino board.

How do you use to receive the data, and piece it together correctly? In this lesson you will learn exactly how to use to receive data from the serial port and stitch it together as one value.

The big picture of serial communication
The serial buffer and Serial.available
Developing a protocol and strategy for reading in data from the serial port
Implement the strategy in Arduino code
BONUS: How to convert the serial data from a string to an integer
Let’s take a step back from, and talk about Serial Communication.

Serial communication is the process of sending one bit of data at a time, sequentially, from one place to another. Like say, sending data from your raspberryPi to a connected Arduino, or vice versa.

USB is one of the most common methods used for serial communication, hence the name Universal Serial Bus. Using Arduino we can easily send and receive data over a USB cable with the built-in Arduino Serial Library.

Now if you don’t know what an Arduino library is, it’s basically a bunch of code that has been bundled together, because it is often used together.

Imagine you were a barber, maybe you have a specific drawer in your barber shop for all your hair cutting tools. Every time somebody walks in for a haircut, you know exactly where to look, in that hair cutting drawer, and all your tools are right there.

Maybe you have another drawer with all the stuff you need for dying peoples hair, when someone walks in and asks to get their hair dyed red, you know exactly which drawer to open. Same thing with Arduino libraries. Arduino libraries put together a bunch of software functions that help you with specific tasks.

For serial communication, we can use the built-in Arduino Serial library.

The Serial library has functions like:

OK, we know that Serial Communication over USB is how we can talk between one device and another, and we know that the Arduino Serial library is the set of tools we’ll use for serial communication. But where does the data that comes from another device actually go on the Arduino?

The answer is the serial buffer, or perhaps more precisely, the serial receive buffer. When bits of data start streaming in from your computer, a piece of hardware on your Arduino called a UART will assemble each of the 8 bits into a byte, and store those bytes for you in the Serial Receive Buffer.

The serial receive buffer can hold 64 bytes.

The data you send from your computer, to your Arduino, will end up in the serial receive buffer.

How do you get this data? That is where comes in.

SERIAL.READ() is a function of the Serial library. What it does is read out the first available byte from the serial receive buffer. When it reads it out, it removes that byte from the buffer.

Say you had sent the phrase “Sub Sandwich” to your Arduino. This means you had put 12 bytes into your serial receive buffer.

If you use…

char myFirstCharacter =;

Then will return the first value available in the serial receive buffer, which in this case is “S”, and it will leave “ub Sandwich” in the Serial receive buffer. Now the value “S” will be stored in the variable myFirstCharacter, and there will only be 11 bytes left in the serial buffer….

If we did this again…

char mySecondCharacter =;

Then mySecondCharacter would be holding the value “u”, and “b Sandwich” would be left in the serial receive buffer. takes one byte at a time from the serial receive buffer.

Now there is a little gotcha here that you need to look out for. Often when sending data over serial, there will be an invisible terminating character added to the end of the transmission.


***About Us:***
This Arduino lesson was created by Programming Electronics Academy. We are an online education company who seeks to help people learn about electronics and programming through the ubiquitous Arduino development board.

***We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.***
Комментариев нет.