Arduino Ultrasonic sensor HC-sr04 max msp
Hey guys, i'm trying to connect my arduino UNO to max msp and trying to get the values from the ultrasonic sensor. i'm getting data from the serial object but can't figure out what it means...
Can anyone point me towards the right direction please? i'm kinda new here...
Gil
Hi Gil, I have the same problem. With the correct arduino program, the sensor work well, but only into arduino. If I try to use Arduino2Max or Maxuino etc. I cant receive values from the digital pins. I'm a new user of arduino, so maybe is just my mistake, but I've no found a solution in the web (many solution, no one working).
Stefano
here's the arduino code for hc-sr04 :
const int TrigPin = 2;
const int EchoPin = 3;
float cm;
void setup()
{
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop()
{
digitalWrite(TrigPin, LOW); //TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //to cm
cm = (int(cm * 100.0)) / 100.0;
Serial.print(cm);
//Serial.print("cm");
Serial.println();
delay(1000);
}
and here's max patch (from arduino example communication graph):
(you might change the serial com to fit your computer,and colse serial monitor before you run max patch)
/* Max/MSP v5 patch for this example
Thanks Liang. have try your sketch and patch. The arduino to max communication is well, but the arduino sketch generate random data (a 50 - 70 cm variation without real changements in the environment).
But, yes, your patch work very well with another sketch by... I'm sorry, I don't remember the author; is not me.
this sketch use the pin 3 (trig) and 4 (echo)
void setup() {
pinMode (2,OUTPUT);//attach pin 2 to vcc
pinMode (5,OUTPUT);//attach pin 5 to GND
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
digitalWrite(2, HIGH);
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(3, OUTPUT);// attach pin 3 to Trig
digitalWrite(3, LOW);
delayMicroseconds(2);
digitalWrite(3, HIGH);
delayMicroseconds(5);
digitalWrite(3, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode (4, INPUT);//attach pin 4 to Echo
duration = pulseIn(4, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(cm); //to get rid of confusion print only one value. Cm will give you a higher range than inches.
Serial.println();
delay(10);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Probably missing a step...
Uploaded sketch to Arduino Uno
Serial appears as: 0.00 (repeating)
(wired Arduino to ping in this way: http://arduinobasics.blogspot.com/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html)
Is the wiring incorrect for this sketch?
Please advise + many thanks!
- M
Answer - yes the wiring was incorrect. Rookie error. Had to connect to the digital arduino pins corresponding to the code.