0% found this document useful (0 votes)
10 views3 pages

Arduino IR Sensor and Motor Control

Uploaded by

srinath05012005
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)
10 views3 pages

Arduino IR Sensor and Motor Control

Uploaded by

srinath05012005
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

int IRSensor = 9; // connect IR sensor module to Arduino pin D9

int LED = 13; // connect LED to Arduino pin 13


void setup(){
[Link](115200); // Init Serial at 115200 Baud Rate.
[Link]("Serial Working"); // Test to check if serial is working or not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
}
void loop(){
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
if (sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the onboard Led
digitalWrite(LED, LOW); // LED LOW
[Link]("Motion Detected!"); // print Motion Detected! on the serial
monitor window
}
else {
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
[Link]("Motion Ended!"); // print Motion Ended! on the serial monitor
window
}
}

IR Sensor using Arduino.


DC MOTOR USING ARDUINO

int motorPin = 9;

void setup() {
pinMode(motorPin, OUTPUT);
[Link](9600);
while (! Serial);
[Link]("Speed 0 to 255");
}

void loop() {
if ([Link]()) {
int speed = [Link]();
if (speed >= 0 && speed <= 255) {
analogWrite(motorPin, speed);
}
}
}

Common questions

Powered by AI

In the DC motor control script, the motor's speed connected to pin 9 is adjusted using the `analogWrite` function based on speed input from the serial monitor. The allowable speed range is between 0 and 255, representing the PWM duty cycle that controls the motor's power and thereby its speed .

Serial communication is used to initialize a baud rate of 115200, allowing for real-time data transmission between the Arduino and external devices like a computer. It helps in debugging and monitoring the IR sensor's output by sending 'Motion Detected!' or 'Motion Ended!' messages depending on the sensor reading through the Serial Monitor .

The Arduino is programmed to read the signal from the IR sensor connected to pin 9. When motion is detected (i.e., the IR sensor outputs a high signal, sensorStatus equals 1), the onboard LED connected to pin 13 is turned off by setting it to LOW, and the serial console outputs 'Motion Detected!'. Conversely, if no motion is detected, the onboard LED is turned on, and the serial console outputs 'Motion Ended!' .

The serial communication with a baud rate of 9600 is a common, reliable speed for transmitting data that balances clarity and speed. It allows for simple interactions and control over the motor speed via the Serial Monitor. However, it's slower compared to higher rates (e.g., 115200), potentially limiting rapid command feedback or responsiveness if high-speed monitoring and operation adjustments are required .

Digital pin manipulation in the IR sensor setup is binary, providing precise ON/OFF control over devices like LEDs based on sensor input. In contrast, PWM in DC motor control allows for nuanced power delivery over a range of values to adjust motor speed, offering more intermediate control compared to the binary nature of digital pin usage. While digital manipulation is sufficient for basic control and feedback systems, PWM is essential for applications requiring variable speed and power modulation .

The `pinMode` function configures pins as either input or output. In this script, pin 9, connected to the IR sensor, is set as an INPUT to read data, while pin 13, connected to the LED, is configured as an OUTPUT to control the LED state based on sensor readings. This setup ensures proper functionality for reading inputs and actuating outputs .

The loop checks the IR sensor status with digitalRead. If the sensor status equals 1 (HIGH), the condition turns the LED off by writing LOW to the LED pin. If the status is 0 (LOW), it turns the LED on by writing HIGH to the LED pin. This toggling logic provides feedback on whether motion is detected or has ended .

The script handles serial data by continuously checking with `Serial.available()` if new data is present. Upon availability, it reads the data as an integer using `Serial.parseInt()` for speed adjustment. This method ensures that only valid integer inputs within the specified range (0-255) affect the motor speed, thereby maintaining consistent and reliable operation .

The script uses `Serial.available()` to check for incoming data. If data is present, `Serial.parseInt()` reads it and checks if the speed value is within the 0-255 range. If the value is outside this range, it is ignored, thus preventing erroneous or harmful signal application to the motor. This basic validation guards against commands that could potentially damage the motor or lead to unexpected behavior .

`AnalogWrite` provides Pulse Width Modulation (PWM) to control motor speed by adjusting the power applied to the motor. When using PWM, considerations such as ensuring the motor's voltage and current ratings are compatible with the Arduino outputs are crucial. The code uses `analogWrite(motorPin, speed);` where `speed` is between 0 and 255, to adjust the motor's speed in correspondence to the PWM signal duty cycle .

You might also like