Go Down

Topic: DUST SENSOR with SERVO MOTOR (Read 540 times) previous topic - next topic

KRISHMAK3R

Hi,

I am trying to develop a project in which if the DUST SENSOR detects the DUST density to be more than
40 ug/m3 then it would flick the Servo motor to flick the Switch ON and vice-versa.

This is the code for it.

I have attached a schematic of the DUST SENSOR with the ARDUINO UNO R3 only. Else the Servo Motor Schematic could be easily understood. I tried it connecting it with my laptop but I think there is a voltage division problem. I am new to Arduino. Please help soon.

/////////////////////////////////////////////////////////////////////////////
// Sharp GP2Y1014AU0F Dust Sensor Demo
//
// Board Connection:
//   GP2Y1014    Arduino
//   V-LED       Between R1 and C1
//   LED-GND     C1 and GND
//   LED         Pin 7
//   S-GND       GND
//   Vo          A5
//   Vcc         5V
//
// Serial monitor setting:
//   9600 baud
/////////////////////////////////////////////////////////////////////////////

// Choose program options.
//#define PRINT_RAW_DATA
#define USE_AVG
#include<Servo.h>
// Arduino pin numbers.
const int sharpLEDPin = 7;   // Arduino digital pin 7 connect to sensor LED.
const int sharpVoPin = A5;   // Arduino analog pin 5 connect to sensor Vo.
int servoPin = 3;
Servo Servol;
// For averaging last N raw voltage readings.
#ifdef USE_AVG
#define N 100
static unsigned long VoRawTotal = 0;
static int VoRawCount = 0;
#endif // USE_AVG

// Set the typical output voltage in Volts when there is zero dust.
static float Voc = 0.6;

// Use the typical sensitivity in units of V per 100ug/m3.
const float K = 0.5;
 
/////////////////////////////////////////////////////////////////////////////

// Helper functions to print a data value to the serial monitor.
void printValue(String text, unsigned int value, bool isLast = false) {
  Serial.print(text);
  Serial.print("=");
  Serial.print(value);
  if (!isLast) {
    Serial.print(", ");
  }
}
void printFValue(String text, float value, String units, bool isLast = false) {
  Serial.print(text);
  Serial.print("=");
  Serial.print(value);
  Serial.print(units);
  if (!isLast) {
    Serial.print(", ");
  }
}

/////////////////////////////////////////////////////////////////////////////

// Arduino setup function.
void setup() {
  // Set LED pin for output.
  pinMode(sharpLEDPin, OUTPUT);
 
  // Start the hardware serial port for the serial monitor.
  Serial.begin(9600);
 
  // Wait two seconds for startup.
  delay(2000);
  Serial.println("");
  Serial.println("GP2Y1014AU0F Demo");
  Serial.println("=================");
  Servol.attach(servoPin);
}

// Arduino main loop.
void loop() { 
  // Turn on the dust sensor LED by setting digital pin LOW.
  digitalWrite(sharpLEDPin, LOW);

  // Wait 0.28ms before taking a reading of the output voltage as per spec.
  delayMicroseconds(280);

  // Record the output voltage. This operation takes around 100 microseconds.
  int VoRaw = analogRead(sharpVoPin);
 
  // Turn the dust sensor LED off by setting digital pin HIGH.
  digitalWrite(sharpLEDPin, HIGH);

  // Wait for remainder of the 10ms cycle = 10000 - 280 - 100 microseconds.
  delayMicroseconds(9620);
 
  // Print raw voltage value (number from 0 to 1023).
  #ifdef PRINT_RAW_DATA
  printValue("VoRaw", VoRaw, true);
  Serial.println("");
  #endif // PRINT_RAW_DATA
 
  // Use averaging if needed.
  float Vo = VoRaw;
  #ifdef USE_AVG
  VoRawTotal += VoRaw;
  VoRawCount++;
  if ( VoRawCount >= N ) {
    Vo = 1.0 * VoRawTotal / N;
    VoRawCount = 0;
    VoRawTotal = 0;
  } else {
    return;
  }
  #endif // USE_AVG

  // Compute the output voltage in Volts.
  Vo = Vo / 1024.0 * 5.0;
  printFValue("Vo", Vo*1000.0, "mV");

  // Convert to Dust Density in units of ug/m3.
  float dV = Vo - Voc;
  if ( dV < 0 ) {
    dV = 0;
    Voc = Vo;
  }
  float dustDensity = dV / K * 100.0;
  printFValue("DustDensity", dustDensity, "ug/m3", true);
  Serial.println("");
  {
    if(dustDensity<=40)
    {
      Servol.write(180);
    }
    else
    {
      Servol.write(0);
    }
  }
} // END PROGRAM


Thanks,
KRISH.
 :) 

Andym535

Why don't you just use a relay module connected to a digital pin?

KRISHMAK3R

Can you please show me how to do it.

Andym535

Buy a suitable relay module, for the voltage and current you need to switch. Connect the module to the arduino and the device you want to control to the output terminals of the relay. it's simple. Google 'Arduino relay" and you'll find loads of how to articles.

Just be careful if you're using mains AC voltage connections...

KRISHMAK3R

I have a two-way relay module with me.

But is there any other way?

KRISHMAK3R


Andym535

There's always another way, but using a relay module would seem to be the easiest here.

Why do you want to use a servo?
What are you trying to switch on and off?
What's its voltage and power rating?
How will you power the Arduino?
What's the spec of the relay module you have?
Does it include an opt-isolator?

KRISHMAK3R

1. to flick a switch
2. a switch connected to two fans and a strip led
3. the servo is a 9G SG90
4. first with my laptop for testing then 9v alkaline battery
5. IDK
6. IDK

Andym535

I meant the voltage and power of the fans and strippings LED, not the servo.

Ignoring the relay/servo question for now, is the dust sensor bit of your code working? What sort of dust density do you see in the serial monitor output?

Go Up