Open Arduino IDE and write code

How it's done

Write following code in Arduino IDE:

int sensor = 2; //Sensor pin

void setup() {
pinMode(sensor,INPUT);
Serial.begin(9600);
}

void loop() {
 int x = digitalRead(sensor);
 Serial.println(x);
}

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