Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Sensors are essential components in the Arduino environment, enabling the creation of interactive projects by allowing the Arduino to interact with the physical world. They convert physical parameters like temperature, light, distance, and pressure into electrical signals that the Arduino can read and process. This article will guide you through the basics of using sensors with Arduino, including practical examples with sample codes.
Sensors can be broadly categorized into analog and digital sensors:
Analog Sensors: These sensors output a continuous voltage that corresponds to the parameter being measured. Examples include temperature sensors like the LM35 and light sensors like the photoresistor.
Digital Sensors: These sensors provide discrete signals, often in the form of binary data. Examples include the DHT11 temperature and humidity sensor and the HC-SR04 ultrasonic distance sensor.
Components Required:
Wiring:
Sample Code:
const int sensorPin = A0; // Pin connected to the sensor's output
float temperature;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin);
temperature = sensorValue * (5.0 / 1023.0) * 100; // Convert the analog reading to temperature
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000);
}
Components Required:
Wiring:
Sample Code:
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
Using sensors with Arduino is a straightforward process that can greatly enhance the functionality of your projects. By understanding the type of sensor and its output, you can effectively integrate it with your Arduino setup to read and process data from the physical world.