Found 203 Articles for Arduino

Find if a substring exists within a string in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:53:17

13K+ Views

In order to check if a specific substring exists within a string in Arduino, the indexOf() function can be used. This returns the index of the first occurrence of the character or a string that you are searching for within another string. In case the character or string does not exist within the given string, the function returns -1.Example implementations are given in the code below −Examplevoid setup() {    Serial.begin(9600);    Serial.println();    String s1 = "Hello World";    Serial.println(s1.indexOf('e'));    Serial.println(s1.indexOf("Wor"));    Serial.println(s1.indexOf('z'));    Serial.println(s1.indexOf("Wer")); } void loop() {    // put your main code here, to run ... Read More

LED Blinking using Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:49:16

443 Views

In order to blink an LED using Arduino, we first connect perform the hardware connections. Choose a pin of your board that supports digital output. We are using the Arduino Uno board, and we will choose pin 7. The circuit will look like this −As you can see, one end of a resistor is connected to pin 7 of Arduino Uno. The other end the resistor is connected to the longer leg (positive) of the LED. The shorter leg of the LED is connected to GND.The value of the resistor can be of the order of 100 Ohms. We'll choose ... Read More

Change the resolution of analogRead in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:48:47

2K+ Views

By default, the analogRead in Arduino follows a 10-bit resolution (this may be different for different boards). However, sometimes, you may not need such a high resolution. Often, people wish to set the resolution to 8-bits, to save on storage. This is because an 8-bit value will take just one byte of storage, whereas anything above that will take two bytes of storage.Luckily, Arduino has an inbuilt function to change the resolution of the analog to digital conversion. The function is analogReadResolution() and it takes in the argument as the resolution in bits.So, if you wish to set an 8-bit ... Read More

Basic analogRead in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:46:52

255 Views

Converting analog values to digital is a common requirement from microcontrollers in general, and Arduino is no different. Arduino IDE has a built-in analogRead function to facilitate the conversion of analog values to digital.From the programming perspective, the only thing you require to know is the pins of your microcontroller that support ADC. On the Arduino UNO board, the pins A0 to A5 support ADC.Now, let us assume that you've connected your A0 pin to an analog wire (maybe the junction between an LDR and a resistor, or the central leg of a potentiometer).The basic Arduino code to print the ... Read More

Access pins in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:46:32

595 Views

In order to access any pin of your board in Arduino, you can simply reference the number of the pin as an integer. On the Arduino boards like Uno, the label is provided for all the I/O pins, and the same label can be used in the code.Thus, both the below expressions are valid −int pin1 = A0; int pin2 = 7;Once the pin number has been defined, depending on your use case, you may need to set the pin as either an input pin or an output pin. This is generally done in the setup (because it is a ... Read More

Using a third-party library in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:42:27

1K+ Views

The general way of using third-party libraries in Arduino is to install them from Tools -> Manage Libraries. We already have a separate post to cover that. However, what if a library you are using for cannot be found in Tools -> Manage Libraries? After all, Manage Libraries only includes those libraries which are present in Arduino Library Manager −http://downloads.arduino.cc/libraries/library_index.jsonSo, how do we use a library not present in Arduino Library Manager? Let's use an example to understand.Consider the TinyGPSPlus library (https://github.com/mikalhart/TinyGPSPlus) which is not available in Arduino's Manage Libraries portal as on 17th March 2021.In order to use this ... Read More

Interrupts in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:41:49

846 Views

What are interrupts?As the name suggests, interrupts are routines that interrupt the normal code flow. An interrupt routine contains a piece of code that the microcontroller on your board should execute whenever an event occurs. Take the air-conditioner example. Suppose it has the following temperature control settings: Switch off cooling whenever temperature reaches 18 degrees C. Now, there will be a temperature sensor that keeps measuring the temperature. Whenever it reports a temperature of 18 degrees Celsius, the normal code running on the AC microcontroller is interrupted, it executes the code to turn off cooling, and then the normal code ... Read More

First Hello World project in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:41:13

7K+ Views

In every language, generally, the first Hello World project is a simple program that prints "Hello World"! We will see what such a code would look like for Arduino. The below code will print "Hello World" on the screen every time your board is powered ON.Examplevoid setup() {    Serial.begin(9600);    Serial.print("Hello World!"); } void loop() { }However, this is not a very good choice of Hello World project for Arduino. We essentially use Arduino for firmware programming (in layperson terms, firmware is the permanent software inside any chip). Now, one characteristic of firmware is to do tasks repeatedly. That ... Read More

For and While loops in Arduino

Yash Sanghvi
Updated on 23-Mar-2021 11:33:58

6K+ Views

The for and while loops in Arduino follow the C language syntax.The syntax for the for loop is −Syntaxfor(iterator initialization; stop condition; increment instruction){    //Do something }Examplefor(int i = 0; i< 50; i++){    //Do something }Similarly, the syntax for the while loop is −Syntaxwhile(condition){    //Do something }Exampleint i = 0 while(i < 50){    //Do something    i = i+1; }The following example will illustrate the working of for and while loops in an Arduino program.Examplevoid setup() {    Serial.begin(9600); } void loop() {    // put your main code here, to run repeatedly:    int i ... Read More

Structure of Arduino Code

Yash Sanghvi
Updated on 23-Mar-2021 11:33:17

188 Views

The Arduino Code has a very simple structure. If you open a new project in the Arduino IDE, you will see the following template prepared for you −Examplevoid setup() {    // put your setup code here, to run once: } void loop() {    // put your main code here, to run repeatedly: }The comments here explain it well. The setup function contains code that will be executed once. This function is executed first when your board reboots. The loop function contains code that will run repeatedly. In other words, if you write 5 lines of code within loop, ... Read More

Advertisements