## Generate random numbers in Arduino

Updated on 29-May-2021 14:02:08
Generating random numbers is one of the key requirements from microcontrollers. Random numbers have several applications. Let’s not get there. You must have an application in mind, which brought you to this page. Generating random numbers is very easy in Arduino, thanks to the inbuilt random() function.Syntaxrandom(min, max)ORrandom(max)where min is 0 by default.Min is inclusive, while max is exclusive. Thus,  random(10, 50) will return a number integer between 10 and 49 (10 and 49 included). random(100) will return a random number between 0 and 99, both included. Note that the random function’s return type is long.Examplevoid setup() {    // put ... Read More

## Trigonometric expressions in Arduino

Updated on 29-May-2021 14:02:29
Arduino provides 3 basic trigonometric functions: sin(), cos() and tan(). All other trigonometric expressions can be derived from these three functions.All the three functions take in angle in radians (type float) as the input. They return a double.For sin() and cos(), the value is between -1 and 1. The value for tan() has no such bounds.ExampleThe example code below illustrates the use of these functions −void setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    float pi = 3.14159;    float angle_deg = 30;    float angle_rad = angle_deg*pi/180;    Serial.println(sin(angle_rad));   ... Read More

## Square and Square root in Arduino

Updated on 29-May-2021 14:02:50
Arduino has support for several popular math functions, square and square root being among them. Let’s look at the square root first.Syntaxsqrt(x)where x is a number of any data type. It returns a double.For square, you ideally shouldn’t need a separate function. You can just multiply the number by itself.x_squared = x*x;However, Arduino does have a separate function for calculating squares. The syntax is −sq(x) where x is a number of any data type. This again returns a double.ExampleThe following example illustrates the use of these functions −void setup() {    // put your setup code here, to run once:   ... Read More

## Timer registers in Arduino

Updated on 29-May-2021 14:03:08
In a previous article, we used the TimerOne library to add timer interrupts to Arduino. But what if we wish to generate timer interrupts without a third-party library? In that case, you will directly have to meddle with the timer registers in Arduino. In this article, we will just introduce the registers relevant to timer operations and explain their significance. We will also provide the page numbers of the ATmega328 (used in Arduino Uno) datasheet wherein you can find detailed information on these registers.You can find the datasheet here −https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdfTCCRxA and TCCRxBThese are timer control registers. The x stands for ... Read More

## Do-while loop in Arduino

Updated on 29-May-2021 14:03:29
The do-while loop’s syntax in Arduino is similar to the syntax in C. It is given below −do{    //Code } while (condition);Note the semicolon at the end.Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    int i = 5;    do{       Serial.println(i);       i--;    } while(i > 0); } void loop() {    // put your main code here, to run repeatedly: }OutputThe Serial Monitor output is shown below −

## Timer1 based PWM in Arduino Uno

Updated on 29-May-2021 14:01:47
In an earlier article, we have seen how PWM can be set on Arduino Uno using the analogWrite() function. Pins 3, 5, 6, 9, 10 and 11 of Arduino Uno can support PWM. The frequency of the square wave is 490 Hz (about 2 ms time period) on all pins except 5 and 6, on which it is 980 Hz (about 1s time period). With analogWrite() you get control over the duty cycle, but not on the frequency of the generated square wave.In this article, we will look at another way of setting PWM in Arduino Uno, specific to Timer1. The advantage ... Read More

## List files stored in SD Card connected to Arduino

Updated on 29-May-2021 13:50:45
As the title suggests, in this tutorial, we will be listing the files stored in an SD Card connected to Arduino.Circuit DiagramThe circuit diagram is shown below −As you can see, you need to make the following connections −SD Card HolderArduino UnoVcc5VGNDGNDMISO12MOSI11SCK13CS10Only for the Vcc, make sure that your SD Card Holder takes 5V as input. If it takes in 3.3V, connect it to the 3.3V pin on Arduino Uno.Code WalkthroughWe will be walking through the example code that comes in with the inbuilt SD library. You can access it from File → Examples → SD → listfilesAlternatively, the code ... Read More

## Append to an existing file stored in SD Card connected to Arduino

Updated on 29-May-2021 13:49:59
In this tutorial, we will, as the title suggests, see how to append to a file in and SD Card connected to Arduino. Actually, it is quite simple. If you have gone through any previous articles on SD Card, then you only need to know thatmyFile = SD.open("example.txt", FILE_WRITE);opens example.txt in the append mode only. Thereafter, myFile.println(dataString);appends to the existing file, and doesn’t overwrite the existing content.If you haven’t gone through any other articles on SD Card, I’d suggest reading the "Store a new file in SD Card connected to Arduino" article. That is a detailed article containing the circuit ... Read More

## Read a file from SD Card connected to Arduino

Updated on 29-May-2021 13:50:20
As the title suggests, in this tutorial, we will read a file from an SD Card connected to Arduino.Circuit DiagramThe circuit diagram is shown below −As you can see, you need to make the following connections −SD Card HolderArduino UnoVcc5VGNDGNDMISO12MOSI11SCK13CS10Only for the Vcc, make sure that your SD Card Holder takes 5V as input. If it takes in 3.3V, connect it to the 3.3V pin on Arduino Uno.Code WalkthroughWe will be walking through the example code that comes in with the inbuilt SD library. You can access it from File → Examples → SD → ReadWriteAlternatively, you can find the ... Read More

## Store a new file in SD Card connected to Arduino

Updated on 29-May-2021 13:51:08
In this tutorial, we will create a new file in an SD Card connected to Arduino Uno.Circuit DiagramThe circuit diagram is shown below −As you can see, you need to make the following connections −SD Card HolderArduino UnoVcc5VGNDGNDMISO12MOSI11SCK13CS10Only for the Vcc, make sure that your SD Card Holder takes 5V as input. If it takes in 3.3V, connect it to the 3.3V pin on Arduino Uno.Code WalkthroughWe will be walking through the example code that comes in with the inbuilt SD library. You can access it from File → Examples → SD → DataloggerAlternatively, you can access the code on ... Read More