Articles on Trending Technologies

Technical articles with clear explanations and examples

Using a third-party library in Arduino

Yash Sanghvi
Yash Sanghvi
Updated on 23-Mar-2021 2K+ 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

First Hello World project in Arduino

Yash Sanghvi
Yash Sanghvi
Updated on 23-Mar-2021 10K+ 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
Yash Sanghvi
Updated on 23-Mar-2021 8K+ 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

Traditional C formatting in Arduino print

Yash Sanghvi
Yash Sanghvi
Updated on 23-Mar-2021 605 Views

If you are familiar with the C language and would like to use the formatting options provided by C language in the Arduino print statements, then there's an easy way to do it. All you need to do is define a character array, and populate it using sprintf(). Within sprintf, you can use the C language formatting. Later, you can simply print out that buffer. The following example code demonstrates how this is done −Examplechar print_buf[100]; void setup() {    Serial.begin(9600); } void loop() {    // put your main code here, to run repeatedly:    for(int i = 0; ...

Read More

Stop Autoscroll in Serial Terminal in Arduino

Yash Sanghvi
Yash Sanghvi
Updated on 23-Mar-2021 2K+ Views

Suppose you have a code like the one below −Examplevoid setup() {    Serial.begin(9600); } void loop() {    // put your main code here, to run repeatedly:    Serial.println(analogRead(A0)); }As you can see, it is continuously printing the results of ADC conversion on A0 pin. Such programs can fill up the Serial Terminal quickly, faster than your eyes can register. For such cases, you may want to simply stop the scrolling in the Serial Monitor. You can do that by unchecking the Autoscroll checkbox at the left bottom of the Serial Monitor Window.OutputOnce that is done, you can read ...

Read More

Print new line and tab in Arduino

Yash Sanghvi
Yash Sanghvi
Updated on 23-Mar-2021 7K+ Views

In order to print a newline, you can either introduce the '' character in your text, or use Serial.println() instead of Serial.print()An example code is given below −Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    Serial.print("This is line1This is line2");    Serial.println("This is line3");    Serial.println("This is line4"); } void loop() {    // put your main code here, to run repeatedly:     }The Serial Monitor output for the above code is −OutputIn order to add a tab space, you can introduce '\t' in your code.An example code is ...

Read More

Reduce decimals while printing in Arduino

Yash Sanghvi
Yash Sanghvi
Updated on 23-Mar-2021 1K+ Views

Often some functions can output really long floating-point numbers, with several decimal digits. Several times, we are just interested in the first couple of decimal digits, and the remaining digits just reduce the readability and make the Serial Monitor window cluttered.In order to round of floating-point numbers when printing to the Serial Monitor, you can just add the number of decimal places required as the second argument to serial.print.An example is shown below −Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println("Printing decimals");    Serial.println(9.6745, 0); //This prints 10    Serial.println(9.6745, 1); ...

Read More

Print binary values in Arduino

Yash Sanghvi
Yash Sanghvi
Updated on 23-Mar-2021 12K+ Views

In order to print binary representation of numbers/ characters in Arduino, you can add 'BIN' as the second argument of your Serial.print() function. Example is shown below −Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    Serial.println(75);    Serial.println(75, BIN);    Serial.println('A');    Serial.println('A', BIN);    Serial.println(1.912, BIN); } void loop() {    // put your main code here, to run repeatedly:     }The Serial Monitor output for the above code can be seen below −As you can see, this works only for integers and characters, and not for floating-point ...

Read More

Print hexadecimal values in Arduino

Yash Sanghvi
Yash Sanghvi
Updated on 23-Mar-2021 10K+ Views

In order to print hexadecimal equivalents of numbers or characters, adding 'HEX' as the second argument of Serial.print() will be sufficient.The following code demonstrates this −Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    Serial.println(75);    Serial.println(75, HEX);    Serial.println('A');    Serial.println('A', HEX); } void loop() {    // put your main code here, to run repeatedly:     }The corresponding Serial monitor output is −Now, the conversion of the decimal number 75 to a hexadecimal value is straightforward and you can even verify that 0x4B is the correct hexadecimal representation ...

Read More

Print plain text in Arduino

Yash Sanghvi
Yash Sanghvi
Updated on 23-Mar-2021 965 Views

To print plain text on the Serial Monitor, the Serial.print() function can be used.In order to use this function, Serial needs to be initialized first (in the setup preferably). A typical implementation is shown below −Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600); } void loop() {    // put your main code here, to run repeatedly:    Serial.print("Hello!");    delay(100); }Note that the argument of Serial.begin() indicates the baud rate. You need to set the baud rate of your serial monitor to this value in order to read the printed messages properly. ...

Read More
Showing 50051–50060 of 61,299 articles
Advertisements