Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Arduino Data Communications

You're reading from   Arduino Data Communications Learn how to configure databases, MQTT, REST APIs, and store data over LoRaWAN, HC-12, and GSM

Arrow left icon
Product type Paperback
Published in Nov 2023
Publisher Packt
ISBN-13 9781837632619
Length 286 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Robert Thas John Robert Thas John
Author Profile Icon Robert Thas John
Robert Thas John
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Part 1:Introduction to Arduino and Sensor Data
2. Chapter 1: Getting Started with Arduino FREE CHAPTER 3. Chapter 2: Leveraging Various Sensors for Data Acquisition 4. Chapter 3: Prototyping with Shields 5. Chapter 4: Storing Data Collected from Sensors 6. Chapter 5: Implementing REST and MQTT Protocols for Communication 7. Part 2:Sending Data
8. Chapter 6: Utilizing Various Communication Technologies 9. Chapter 7: Communicating with LoRaWAN 10. Chapter 8: Working with Ethernet 11. Chapter 9: Leveraging Cellular Communication Technology 12. Chapter 10: Communicating via HC-12 13. Chapter 11: Managing Communication with RS-485 14. Part 3: Miscellaneous Topics
15. Chapter 12: Enhancing Security for Reducing Risk 16. Chapter 13: Scaling for High Availability 17. Chapter 14: Building and Manufacturing Hardware 18. Index 19. Other Books You May Enjoy

Learning about the Arduino programming language

The Arduino programming language is a subset of the C/C++ programming language. It’s okay if you haven’t written C/C++ before now, as getting started with Arduino is pretty straightforward.

Getting started with Arduino

The quickest way to get started is to click on the File menu on the Arduino IDE, then choose New. Doing that will give you a new file with a structure similar to the one in the following code block, which does nothing but give you a template to add instructions in:

void setup() {
}
void loop() {
}

The preceding code is made up of two sections:

  • The first section, void setup() {}, holds code that will be run exactly once when the microcontroller boots up. The code that gets executed is contained within the opening and closing curly brackets. Here is the code again:
    void setup() {
    }
  • The next section of code, void loop() {}, holds code that is executed repeatedly as long as the microcontroller is powered up. The code is also contained within the curly brackets. Here is the code again:
    void loop() {
    }

Let’s proceed to discuss GPIO pins.

General purpose input and output

A lot of the interaction that happens on a microcontroller is through the general purpose input and output (GPIO) pins. There are two categories of pins: digital and analog. The following pinout diagram for the MKR WiFi 1010 shows the pins.

Figure 1.4 – Arduino MKR WiFi 1010 pinout diagram (source – https://docs.arduino.cc/static/9d6534bb9783a42e3527a8c03472ad10/ABX00023-full-pinout.pdf)

Figure 1.4 – Arduino MKR WiFi 1010 pinout diagram (source – https://docs.arduino.cc/static/9d6534bb9783a42e3527a8c03472ad10/ABX00023-full-pinout.pdf)

The pin labels are color-coded to make it easier to identify pins by their function. Some pins have only one use, such as the Power and Ground pins. Other pins have multiple uses. Pay attention to the labels that are closer to the board on the left and right. You will see that 22 pins can be used as digital pins, while 7 of those can be used as analog pins in addition. However, no pins are labeled as input or output.

Let’s discuss how to configure pins for input or output depending on whether the pin is digital or analog.

Digital pins

Digital pins represent a state that depends on the amount of electricity flowing through them. There are two states:

  • Low state: This represents an OFF state or a FALSE state. This is set when the amount of electricity is below a certain reference.
  • High state: This represents an ON state or a TRUE state. This is set when the amount of electricity is above a certain reference.

A digital pin is configured as either of the following:

  • An input pin, used for reading the state of a peripheral attached to it. An example of this is a button. You can read the state of an input pin using the digitalRead() function.
  • An output pin, used to control the state of a peripheral attached to it. An example of this is an LED. You can set the state of an output pin using the digitalWrite() function.

You can configure a digital pin using the pinMode() function. The first parameter is the pin number, and the second is one of INPUT, OUTPUT, or INPUT_PULLUP. You will see this used in the Hello World example very soon.

Analog pins

Analog pins can hold a state between 0 and 1023, which is proportional to the voltage passing through the pin. There are two possible directions:

  • Input pins: These are connected to peripherals that send a voltage value over a range. This is how peripherals such as sensors work. You can read the value of the pin using the analogRead() function.
  • Output pins: These are connected to peripherals that can operate over a variable range, such as electric motors and actuators. You can set the value of the pin using the analogWrite() function.

You are now ready to write your first Arduino program.

You have been reading a chapter from
Arduino Data Communications
Published in: Nov 2023
Publisher: Packt
ISBN-13: 9781837632619
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime