Home / Codes / Using Ternary Operator in Arduino

Using Ternary Operator in Arduino

If your Arduino code involves decision making, then it’s very likely you’re using conditional statements. Using if … else… is common;  switch statements can be used as well. But there is a third way, using ternary operator, that will really make your code shorter.

Example Scenario

Say we are reading the state of a (active low) switch, wired to pin 2 of an Arduino. The switch controls a LED on pin 13, that is if the switch is closed, the LED turns on, and so on. The circuit is shown in the next section.

The if … else… statement for this scenario would be like this:

if(digitalRead(2) ==  LOW){
  digitalWrite(13, HIGH);
}else{
  digitalWrite(13, LOW);
}

We can actually make this block a single line of code!

Ternary Operator

The ternary operator in Arduino programming is the “?” operator and is used like this:

expressionA ? expressionB : expressionC

The way to read it is “is expressionA true? if it is then execute expressionB, otherwise, execute expressionC”.

Going back to our scenario, the ternary operator equivalent of the if…else… statement is:

digitalRead(2) ? digitalWrite(13, LOW) : digitalWrite(13, HIGH);

Here, digitalRead(2) returns either LOW or HIGH which are equivalent to logic FALSE or TRUE respectively. Hence, if the switch is closed, then digitalRead(2) returns FALSE, and digitalWrite(13, HIGH) is executed. Otherwise, the LED is turned off.

Example circuit

Simulate this circuit

Another Example Scenario

You can also use the ternary operator to quickly assign a value to a variable given a condition. For example, you have a temperature sensor attached to analog pin 0. But you only need to acquire the sensor value if it’s greater than a certain amount, say, 50 degrees. The code will now be:

int acq_temp = (sensor_temp > 50) ? sensor_temp : 0;

Here, acq_temp will have a value equal to sensor_temp if it’s greater than 50, otherwise, acq_temp will be zero. This assumes that sensor_temp is another int variable.

Nested Ternary Operators

Ternary operators can also be nested, and will be equivalent to an if…else if…else… statement. Say we want to implement the logic XOR function using two switches wired to an Arduino.

The XOR function has a truth table:

XOR Truth TableBasically, the LED should only turn on when only one of the switches is closed. Other than that, the LED stays off.

We attach the second switch to pin 3 of the Arduino while the switch on pin 2 and the LED on pin 13 remains.

Nested ternary example circuit

Simulate this circuit

The statement would now be:

!digitalRead(2) ? digitalWrite(13, HIGH) : digitalRead(3) ? digitalWrite(13, LOW) : digitalWrite(13, HIGH);

The first part of this statement checks the state of pin 2. Since there is a “!” negate operator, this statement returns true if the switch is closed. Hence, the LED now turns on. Otherwise, the state of pin 3 is read next. If pin 3 is low, then the LED now turns on. Otherwise, the LED stays off.

Here’s another way, this time, without using the negate operator:

 digitalRead(2) ? (digitalRead(3) ? digitalWrite(13, LOW) : digitalWrite(13, HIGH)): digitalWrite(13,HIGH);

Nevertheless, both statements are equivalent to:

if (digitalRead(2) == LOW){
  digitalWrite(13, HIGH);
}else if (digitalRead(3) == LOW){
  digitalWrite(13, HIGH);
}else {
  digitalWrite(13, LOW);
}


Hopefully, this article has helped you in any way. Happy coding!

Check Also

moving average filter

Implementing an 8-point Moving Average Filter

If you read sensors using microcontrollers before, you have dealt with data that is always …

Leave a Reply

Your email address will not be published. Required fields are marked *