Found 112 Articles for Arduino Boards

Controlling a DC Motor with Arduino

Yash Sanghvi
Updated on 31-May-2021 14:41:28

389 Views

A DC Motor is the simplest kind of motor. It has two terminals or leads. When connected with a battery the motor will rotate, and if the connections are reversed, the motor will rotate in the opposite direction. If the voltage across the terminals is reduced, the motor speed will reduce accordingly.In this article, we will see how to interface a DC Motor with Arduino and control its speed. We won’t be looking at reversing the direction of the motor, as that will require an additional IC (an H-bridge). At the end of this article, I’ll provide links to some ... Read More

Detach interrupts from a source in Arduino

Yash Sanghvi
Updated on 31-May-2021 14:42:30

938 Views

We have seen that in order to attach interrupts to a source, we use the .attachInterrupt() function, with the required arguments.For example, for attaching the interrupts to a specific pin, we useattachInterrupt(digitalPinToInterrupt(pin), ISR, mode);In the same way, to detach the interrupt from a source, we can call the detachInterrupt() function. This will simply disable that particular interrupt. The recommended syntax for disabling pin interrupts is −detachInterrupt(digitalPinToInterrupt(pin))where pin is the pin number on which you wish to disable the interrupt.

Enable and disable interrupts in Arduino

Yash Sanghvi
Updated on 31-May-2021 14:42:53

6K+ Views

If you wish to disable interrupts (when executing some critical piece of code, especially one which should be completed within a given time period), you can do that with the help of the noInterrupts() function.Once your critical code has executed and you wish to re-enable the interrupts, you can do that using interrupts() function. Note that interrupts are enabled by default in Arduino, and therefore, calling interrupts() without an initial call to noInterrupts() is unnecessary.ExampleThe general structure of a code containing noInterrupts() and interrupts() is given below −void setup() {    // put your setup code here, to run once: } ... Read More

Check if a character is printable in Arduino

Yash Sanghvi
Updated on 31-May-2021 14:43:23

298 Views

Through various operations, you may come across characters which are not printable. After all, a char is an 8-bit number, and if you look at the ASCII table (https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html), only the values from 32 to 127, or a total of 96 values out of 127 are printable (see http://facweb.cs.depaul.edu/sjost/it212/documents/ascii-pr.htm).  ASCII uses only 7 digits, instead of 8.Thus, if you get a char output from a function, and wish to check if it is printable, then you can use the isPrintable() function of Arduino.SyntaxisPrintable(myChar)where myChar is the character being checked. This function returns a true if the character is printable.Examplevoid setup() { ... Read More

Map a 10-bit number to 8-bit in Arduino

Yash Sanghvi
Updated on 31-May-2021 14:45:03

840 Views

Mappings often have to be performed in Arduino for a variety of reasons. One example would be mapping the 10-bit ADC output to 8-bit to save on storage. A 10-bit number would occupy 2-bytes for storage, whereas an 8-bit number would occupy just one byte and still preserve most of the information of the 10-bit number.Arduino has a readymade map() function for achieving this.Syntaxmap(value, fromLow, fromHigh, toLow, toHigh)where, value is the value to be mapped; fromLow and fromHigh are the bounds of the range of the current value; toHigh and toLow are the bounds of the range of the new ... Read More

Clear/Set a specific bit of a number in Arduino

Yash Sanghvi
Updated on 31-May-2021 14:38:36

990 Views

When you delve into advanced firmware, you deal with a lot of registers, whose specific bits need to be set or cleared depending on your use case. Arduino has inbuild functions to do that.SyntaxbitSet(x, index)and, bitClear(x, index)Where x is the number whose bit has to be set/ cleared and index is the position of the bit (0 for least significant or right-most bit). This function makes changes in the number x in place, and also returns the updated value of x.Note that setting a bit means setting its value to 1, and clearing it means setting its value to 0.ExampleThe ... Read More

Constrain a number within a given range in Arduino

Yash Sanghvi
Updated on 31-May-2021 14:18:08

4K+ Views

The constrain() function in Arduino helps to, as the name suggests, constrain a number between an upper bound and a lower bound.Syntaxconstrain(val, min, max)where, val is the number to be constrained, min is the lower bound value, and max is the upper bound valueIf val is less than min, this function will return min. If val is greater than max, this function will return max. As long as val is between min and max, this function will return val.ExampleThe following example illustrates the use of this function −void setup() {    // put your setup code here, to run once: ... Read More

Bitwise Right/ left shift numbers in Arduino

Yash Sanghvi
Updated on 31-May-2021 14:17:17

1K+ Views

If you are a firmware developer, then shifting numbers, or registers by certain number of bits may be quite common for you. In Arduino as well, the same bit shift operators can be used, that are used in the C language, namely > for right shift.Syntaxx >> n or x > 1);    Serial.println(x

Modulo / Remainder in Arduino

Yash Sanghvi
Updated on 31-May-2021 14:16:04

7K+ Views

The modulo operator in Arduino is exactly the same as in C language, or most other languages for that matter. The operator is %. The syntax is: a % b and it returns the remainder when a is divided by b.ExampleThe following example illustrates the use of this operator −void setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    Serial.println(10%3);    Serial.println(4%2);    Serial.println(50%9); } void loop() {    // put your main code here, to run repeatedly: }OutputThe Serial Monitor output is shown below. You can work out the remainders yourself and verify that the output is correct.

Check if a character is Space/Whitespace in Arduino

Yash Sanghvi
Updated on 31-May-2021 14:15:32

521 Views

The isSpace() and isWhitespace() functions can be used to check if a character is a space or, more specifically, a whitespace. Whitespace is a subset of space. While whitespace includes only space and horizontal tab (‘\t’), space includes form feed (‘\f’), new line (‘’), carriage return (‘\r’) and even vertical tab (‘\v’).ExampleThe following example demonstrates the usage of these functions −void setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    char c1 = 'a';    char c2 = ' ';    char c3 = '\t';    char c4 = '';   ... Read More

Advertisements