0% found this document useful (0 votes)
0 views2 pages

Programming

The document contains code snippets for various sensors and components including a soil moisture sensor, relay module with a DC motor, ultrasonic sensor, and a potentiometer controlling an LED. Each section initializes the respective components, reads sensor values, and outputs data via serial communication. The code demonstrates basic functionality such as reading analog values, controlling relay states, and adjusting LED brightness based on potentiometer input.

Uploaded by

mediapriscilla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Programming

The document contains code snippets for various sensors and components including a soil moisture sensor, relay module with a DC motor, ultrasonic sensor, and a potentiometer controlling an LED. Each section initializes the respective components, reads sensor values, and outputs data via serial communication. The code demonstrates basic functionality such as reading analog values, controlling relay states, and adjusting LED brightness based on potentiometer input.

Uploaded by

mediapriscilla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SOIL MOISTURE SENSOR

int sensorPin = A0; // Analog pin connected to sensor


int sensorValue = 0;
void setup() {
[Link](9600); // Start serial communication
}
void loop() {
sensorValue = analogRead(sensorPin); // Read sensor value
[Link]("Soil Moisture Value: ");
[Link](sensorValue);
delay(1000); // Wait 1 second
}
RELAY MODULE & dc motor
int relayPin = 7;
void setup() {
pinMode(relayPin, OUTPUT);
}
void loop() {
digitalWrite(relayPin, HIGH); // Relay ON
delay(2000); // Wait 2 seconds
digitalWrite(relayPin, LOW); // Relay OFF
delay(2000); // Wait 2 seconds
}
ULTRASONIC SENSOR
int trigPin = 9;
int echoPin = 10;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
[Link](9600);
}
void loop() {
// Clear trigger
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send 10µs pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read echo time
duration = pulseIn(echoPin, HIGH);

// Calculate distance
distance = duration * 0.034 / 2;

[Link]("Distance: ");
[Link](distance);
[Link](" cm");

delay(500);
}

potentiometer
int potPin = A0; // Potentiometer middle pin
int ledPin = 9; // PWM pin for LED
int potValue = 0;
int brightness = 0;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
potValue = analogRead(potPin); // Read (0–1023)
brightness = map(potValue, 0, 1023, 0, 255); // Convert to PWM

analogWrite(ledPin, brightness); // Set LED brightness

delay(10);
}

You might also like