A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.makeuseof.com/how-to-code-your-arduino-board/ below:

How to Code Your Arduino Board

How to Code Your Arduino Board

Sign in to your MUO account

Image from Unsplash -- No attribution required
https://unsplash.com/photos/__LXdDYiL-w

With a budding community always making easy-to-follow and fun projects, you'll never run out of ideas for what you can do with an Arduino microcontroller board.

That said, the most important part of any Arduino project is the code that runs everything. Programming your Arduino right is the key to ensuring a properly working electronics project. But how do you code Arduino?

What Is an Arduino? Image from Unsplash -- No attribution required
https://unsplash.com/photos/LC_SOISs-74

Arduino is an open-source prototyping platform. It's easy to use, features a GPIO header for connecting sensors and other electronic components, and has a relatively simple programming language. The boards come in different shapes and sizes, from as small as the Arduino Nano for deployable projects to the Arduino Mega 2560 for projects with more hardware. Read our Arduino beginner's guide for more information on the platform.

How to Program an Arduino

Programming an Arduino is as simple as plugging hardware into one. All you need is an Arduino board, an appropriate USB cable (check which type of USB port your Arduino has), and a computer to get started. You'll be using the Arduino Programming Language based on C++.

While it has a downloadable IDE, you can also use your web browser to code Arduino. Do keep in mind that you'll have to install the Arduino Agent if you're using the browser-based IDE – the first time you try it, you'll be asked to download and install the Agent before you can get to coding.

Components of an Arduino Program

Arduino programs are called sketches. They're usually written in two main functions:

As you would with any other program or script, any important libraries and values are declared and imported before the two functions mentioned above. Based on your requirements, you're free to add more functions if you'd like.

Screenshot uploaded by Yadullah Abidi. No attribution required. 

You can use the Serial Monitor to see the data that your Arduino is sending over the serial USB connection. The Serial Monitor is also used for interacting with the board using your computer or other capable devices. It also includes a Serial Plotter which can plot your serial data for better visual representation.

Using Basic Components With Arduino

We'll be making a small setup where the Arduino can read the input of a button and light an LED based on whether it's pressed or not. Before we get to coding though, we need to wire up our hardware. You'll need the following items:

Follow the wiring diagram below to connect everything properly. Pay special attention to the GPIO (General Purpose Input Output) pin that each wire connects to on the Arduino board.

Screenshot uploaded by Yadullah Abidi. | No attribution required. 

Once all the hardware is wired up, go ahead and copy-paste the following code into the online IDE. You'll find comments throughout the code to better explain what each part is doing.

        #define LED_PIN 8 
#define BUTTON_PIN 7

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
}


void loop() {
  if (digitalRead(BUTTON_PIN) == HIGH) {
    digitalWrite(LED_PIN, HIGH);
  }
  else {
    digitalWrite(LED_PIN, LOW);
  }
}

There's more to the Arduino than just LEDs and buttons though. Let's go over some more advanced code that integrates a distance sensor and buzzer instead.

Using an Ultrasonic Sensor With Arduino

Your Arduino can read sensor data and interact according to its surroundings. We're going to hook up an HC-SR04 ultrasonic distance measuring sensor and a buzzer to the board to make a proximity alarm.

Screenshot uploaded by Yadullah Abidi. No attribution required. 

Here's what the code looks like; you'll find line-by-line explanations throughout.

        
const int TRIG_PIN = 6;
const int ECHO_PIN = 7;
const int LED_PIN = 3;
const int DISTANCE_THRESHOLD = 50;
float duration_us, distance_cm;
void setup() {
 Serial.begin (9600);
 pinMode(TRIG_PIN, OUTPUT);
 pinMode(ECHO_PIN, INPUT);
 pinMode(LED_PIN, OUTPUT);
}

void loop() {


 
 digitalWrite(TRIG_PIN, HIGH);
 delayMicroseconds(10);
 digitalWrite(TRIG_PIN, LOW);
 duration_us = pulseIn(ECHO_PIN, HIGH);
 
 distance_cm = 0.017 * duration_us;

 if(distance_cm < DISTANCE_THRESHOLD)


   digitalWrite(LED_PIN, HIGH);
 else
   digitalWrite(LED_PIN, LOW);
 Serial.print("distance: ");
 Serial.print(distance_cm);
 Serial.println(" cm");

 delay(500);


}
How to Run an Arduino Program?

Now that you're ready with the hardware and code, it's time to upload the code to your Arduino. Follow these steps.

  1. Click the Verify checkmark button to compile your code and ensure it's free of errors.
  2. Select the Arduino board and its corresponding COM port from the dropdown menu.
  3. Click the Upload button and wait for the code to finish uploading.
Screenshot uploaded by Yadullah Abidi. No attribution required. 

As soon as you click the Upload button, you'll start seeing activity in the black console window underneath. Assuming your Arduino is working and properly connected, your code will be uploaded and you can start testing out your project.

Microcontrollers Can Be Fun

Microcontrollers like Arduino are a great way to get into the world of DIY electronics. Arduino for kids is an especially great activity. Once you learn how to code Arduino, more powerful boards like the Raspberry Pi open up an entirely different world of opportunities in terms of what you can build with just a few basic sensors and some lines of code.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4