Bit mask with Arduino on UNO

Updated on 26 January 2023
dev board Arduino UNO
firmware Arduino
chip ATmega328P
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Pre-requisites

Try these simpler or similar examples:

Buy the components

Code

Download code bitmask-arduino-uno.ino
byte transmit = 7;  // define our transmit pin
byte data = 170;    // value to transmit, binary 10101010
byte mask = 0b00000100;  // (0x04) which corresponds to the bit 2
byte bitDelay = 100;

void setup() {
  pinMode(transmit, OUTPUT);
  Serial.begin(115200);

  Serial.print("data: ");
  Serial.print(data);
  Serial.print("   ");
  Serial.println(data, BIN);

  Serial.print("1 << 10101010:  ");
  Serial.println(1 << data, BIN);

  Serial.print("1 >> 10101010:  ");
  Serial.println(1 >> data, BIN);

  Serial.print("10101010 >> 1:  ");
  Serial.println(data >> 1, BIN);

  Serial.print("10101010 << 1:  ");
  Serial.println(data << 1, BIN);
  Serial.println("\n");

  for (mask = 00000001; mask > 0; mask <<= 1) {  // iterate through bit mask
    Serial.print("mask: ");
    Serial.print(mask, BIN);
    Serial.print("   data: ");
    Serial.print(data, BIN);
    Serial.print("   data & mask: ");
    if (data & mask) {               // if bitwise AND resolves to true
      digitalWrite(transmit, HIGH);  // send 1
      Serial.println("HIGH");
    } else {                        // if bitwise and resolves to false
      digitalWrite(transmit, LOW);  // send 0
      Serial.println("LOW");
    }
    delayMicroseconds(bitDelay);    // delay
    Serial.println("-------------");
  }

  Serial.println("\n");
  data = 170;
  mask = 0b00000100;
  Serial.print("data:  ");
  Serial.println(data, BIN);
  Serial.print("mask:  ");
  Serial.println(mask, BIN);

  Serial.print("Turn on pin 2 (data |= mask):    ");
  data |= mask;
  Serial.println(data, BIN);
  Serial.print("Turn off pin 2 (data &= ~mask):  ");
  data &= ~mask;
  Serial.println(data, BIN);
}

void loop() { }

Makefile

BOARD?=arduino:avr:uno
PORT?=/dev/cu.usbmodem14*
BUILD=build

.PHONY: default lint all flash clean

default: clean lint all flash clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright *.ino

all:
	arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./

flash:
	arduino-cli upload --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)

clean:
	rm -rf build

Prototype

A photo of the actual setup.

Bit mask with Arduino on UNO prototype

Serial console

Serial output from the firmware.

Bit mask with Arduino on UNO serial console

Description

This simple code shows some principles of bit masking.

References