Open Arduino IDE and write code

How it's done

Write following code in Arduino IDE:

int sensor = 2; //Sensor pin

void setup() {

void loop() {
 int x = digitalRead(sensor);

Extra info:

Code explanation:

int sensor = 2; //Sensor pin define which pin we use to read sensor value.

pinMode(sensor,INPUT); // Define that sensor pin will be used as input (to read).

Serial.begin(9600); // Allow us to read data on serial monitor.

int x = digitalRead(sensor); // Declare integer variable x and initialize it with digital read value from sensor.

Serial.println(x); // Print value of x variable in serial monitor with line break.

Activity Q&A

Need help with this activity?Add the first post