Go Down

Topic: Blinking LEDS with motion sensor (Read 3507 times) previous topic - next topic

TheVerse

this is what I was told by the tech guy from the shop

"You can still use this sensor without using the serial interface with your Arduino. There is an analog output (10mV/inch) which you can easily read with an analog input pin on your Arduino"


Grumpy_Mike

Ok I missed that bit on the spec sheet. That makes it a lot simpler.

TheVerse

he has written back and said

"On closer inspection of the data sheet it looks as though the serial output is at the TTL level"


AWOL

#33
Sep 29, 2011, 06:31 pm Last Edit: Sep 29, 2011, 06:36 pm by AWOL Reason: 1
My reading of it is that there's an analogue output, a PWM TTL output (pulse width proportional to range) and an RS232 interface, I guess for setup.

Edit: scratch that, the RS232 is output only, with range messages.
I'd go with the PWM output and use pulseIn to time it.
That is a confusing data sheet!
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

TheVerse

ok, so i am really confused now, has anyone got an example of how it would be connected to the arduino

AWOL

Just connect a digital input pin on the Arduino to "PW" on the sensor, connect power and ground and use "pulseIn".
Or, connect an analogue input on the Arduino to "AN" on the sensor, connect power and ground and use "analogRead"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

TheVerse

so i dont need to purchase an RS232 Shifter?

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


TheVerse

hi all, so I got my maxsonar EZ0, and i am getting readings from it using Analogue, I found this example which lights up the LED as u get closer and gave it a try

/*
From the datasheet of the The LV-MaxSonar-EZ0 the output analog
voltage with a scaling factor of (Vcc/512) per inch.
A supply of 5V yields ~9.8mV/in. and 3.3V yields ~6.4mV/in.
The output is buffered and corresponds to the most recent range data.
http://www.maxbotix.com/uploads/LV-MaxSonar-EZ0-Datasheet.pdf
*/

// pin connected to analog output on maxsonar sensor
int sonarPin = 0;
// pin connected to digital output on red led
int ledPin = 7;

void setup() {
  Serial.begin(9600);  // sets the serial port to 9600
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // The Arduino's analog-to-digital converter (ADC) has a range of 1024,
  // which means each bit is ~4.9mV.
  // Therefore, to convert the number returned by the ADC to inches, wet
  // have to divide by 2.
  // To convert to cm is necessary to divide by 0.3937.
  // 1 centimeter = 0.3937 inch
  int distance = analogRead(sonarPin)/(2*0.3937);

  Serial.print(distance, DEC); // prints the value read
  Serial.println("cm");
  // If something is at a distance less than 20 cm than LED lights up.
  if(distance > 300) {
    digitalWrite(ledPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(ledPin, LOW);
    delayMicroseconds(500);
  }
}

it seems to work well, but when I look at the numbers in the serial monitor, the numbers get higher as I get closer, also the readings can be a little erratic, can someone help me modify this to a PW, when I tried i just got no readings at all

TheVerse

#40
Oct 18, 2011, 08:56 pm Last Edit: Oct 18, 2011, 09:00 pm by TheVerse Reason: 1
ok, so I have made some progress and managed to merge some code which works for detecting movement, Grumpy Mike wrte some code for me which was for the Sharp sensor, I am struggling to get the code implemented into my sketch, here is what I have sketched to detect movement and turn the LED on, please help me to change it so that the LEDs are blinking and then the blinking speeds up as the movement is detected


/*
From the datasheet of the The LV-MaxSonar-EZ0 the output analog
voltage with a scaling factor of (Vcc/512) per inch.
A supply of 5V yields ~9.8mV/in. and 3.3V yields ~6.4mV/in.
The output is buffered and corresponds to the most recent range data.
http://www.maxbotix.com/uploads/LV-MaxSonar-EZ0-Datasheet.pdf
*/

// pin connected to analog output on maxsonar sensor
int sonarPin = 7;
// pin connected to digital output on red led
int ledPin = 9;

void setup() {
  Serial.begin(9600);  // sets the serial port to 9600
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // The Arduino’s analog-to-digital converter (ADC) has a range of 1024,
  // which means each bit is ~4.9mV.
  // Therefore, to convert the number returned by the ADC to inches, wet
  // have to divide by 2.
  // To convert to cm is necessary to divide by 0.3937.
  // 1 centimeter = 0.3937 inch
  int distance = pulseIn(sonarPin, HIGH)/(2*0.3937);

  Serial.print(distance, DEC); // prints the value read
  Serial.println("cm");
  // If something is at a distance less than 20 cm than LED lights up.
  if(distance < 10000) {
    digitalWrite(ledPin, HIGH);
    delayMicroseconds(5000);
    digitalWrite(ledPin, LOW);
    delayMicroseconds(5000);
  }
}
attached is the breadboard image

AWOL

It would be useful if you could re post that properly, without the italics.
Use the # icon on the editor's toolbar.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

TheVerse

#42
Oct 18, 2011, 09:33 pm Last Edit: Oct 18, 2011, 09:35 pm by TheVerse Reason: 1
ok, so I have made some progress and managed to merge some code which works for detecting movement, Grumpy Mike wrote some code for me which was for the Sharp sensor, I am struggling to get the code implemented into my sketch, here is what I have sketched to detect movement and turn the LED on, please help me to change it so that the LEDs are blinking and then the blinking speeds up as the movement is detected
Code: [Select]
/*
From the datasheet of the The LV-MaxSonar-EZ0 the output analog
voltage with a scaling factor of (Vcc/512) per inch.
A supply of 5V yields ~9.8mV/in. and 3.3V yields ~6.4mV/in.
The output is buffered and corresponds to the most recent range data.
http://www.maxbotix.com/uploads/LV-MaxSonar-EZ0-Datasheet.pdf
*/

// pin connected to PW output on maxsonar sensor
int sonarPin = 7;
// pin connected to digital output on red led
int ledPin = 9;

void setup() {
 Serial.begin(9600);  // sets the serial port to 9600
 pinMode(ledPin, OUTPUT);
}

void loop() {
 // The Arduino's analog-to-digital converter (ADC) has a range of 1024,
 // which means each bit is ~4.9mV.
 // Therefore, to convert the number returned by the ADC to inches, wet
 // have to divide by 2.
 // To convert to cm is necessary to divide by 0.3937.
 // 1 centimeter = 0.3937 inch
 int distance = pulseIn(sonarPin, HIGH)/(2*0.3937);

 Serial.print(distance, DEC); // prints the value read
 Serial.println("cm");
 // If something is at a distance less than 20 cm than LED lights up.
 if(distance < 10000) {
   digitalWrite(ledPin, HIGH);
   delayMicroseconds(5000);
   digitalWrite(ledPin, LOW);
   delayMicroseconds(5000);
 }
}





attached is the breadboard image

AWOL

You're not using the A/D converter, so the comments about it are a waste of space.
It is a useful side effect that pins default to inputs, because otherwise you'd have to write another pinMode.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

TheVerse

sorry, I will re-edit the comments. I am a contemporary artist, and I have no background in electronics at all, rather than just pay someone to do it for me I am doing my best to learn. I know some of the things I have asked are really obvious, but I am really new to this and doing my best  :~ but appreciate the help I have gotten of this forum.

Go Up