Been plugging away at my very first project which is taking a SR04 sensor reading and if a certain threshold is met, a tone is played until the criteria is no longer met. The audio tone is played thru a 3.5mm stereo jack which is plugged into an iPad which reads the audio signal as a count... 1, 2, 3, etc. The solution seems to work well both on and off a breadboard but only when plugged into a USB power source such as a laptop or PC. The problem comes in when I plug into a 110v receptacle... I don't know how else to describe it other than to say the signal looks noisy and the iPad app (which is a custom build) has a hard time distinguishing between the tone and general noise. Now the app does have an audio sensitivity slider adjustment but it has to be so precise that it doesn't make the solution practical. The solution I am currently working on is trying to figure out how to either applify the tone to make it much louder, or cleaning up the noise by introducing a resistor or capacitor or something (very noob). I'm also looking at an SD shield with a custom wav file which so far has tested really badly.
So I guess my question is does anybody out there have experience dealing with such a problem? Is the audio output from an Arduino notoriously noisy? I'm including my code wit this post just in case I have a problem in there.
Any and all help is greatly appreciated. Thanks for taking the time...
/* HC-SR04 Sensor
https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.
The circuit:
* VCC connection of the sensor attached to +5V
* GND connection of the sensor attached to ground
* TRIG connection of the sensor attached to digital pin 2
* ECHO connection of the sensor attached to digital pin 4
Original code for Ping))) example was created by David A. Mellis
Adapted for HC-SR04 by Tautvidas Sipavicius
This example code is in the public domain.
*/
const int trigPin = 12;
const int echoPin = 13;
int pinSpeaker = 11;
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(pinSpeaker, OUTPUT); // declare speaker as output
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: 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(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
if (cm < 20){
tone(pinSpeaker, 500, 200);
}
}
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;
}
You need to spend a bit of time describing the noise - is it random noise, 60 (or 120) hz pulses or just what? As Terry points out, ground loops are a possibility too - which devices are running on batteries and which are somehow connected (via charger etc.) to the mains?
Can you tell us all the power sources for Arduino and the Ipad are?
This sounds like a ground loop or induced noise issue.
Sorry... power sources. Arduino is plugged into an outlet with a ZJChao AC/AD Adapter model 0910 rated at 9V 1A, and the iPad is plugged in with it's lightning port to an Apple 12W USB Power Adapter. If I unplug the iPad and let it run on battery I get the same noisy results.
Without a description of the noise, it is tough to tell, but if the iPad is on batteries (and has no other connection to it), my first guess is a noisy power supply to the Arduino. Try a different supply - see if that changes things.
gpsmikey:
You need to spend a bit of time describing the noise - is it random noise, 60 (or 120) hz pulses or just what? As Terry points out, ground loops are a possibility too - which devices are running on batteries and which are somehow connected (via charger etc.) to the mains?
Nothing is running on batteries and I'm not sure how to capture and process the signal for the noise but I will try. Only the Sr04's VCC pin is connected to the 5v on the arduino. How can I try to identify and test for a ground loop?
Tourless:
Nothing is running on batteries and I'm not sure how to capture and process the signal for the noise but I will try. Only the Sr04's VCC pin is connected to the 5v on the arduino. How can I try to identify and test for a ground loop?
"Nothing is running on batteries" but then you say "If I unplug the iPad and let it run on battery I get the same noisy results" so which is it?
Ok so I'm digging around for a different, comparable power supply... to that end I need to know what my limits are... I don't want to fry my board. Will a 9V 1500mA work for me? what about a 9V 300mA? 9V 700mA? Is a 9V 1000mA the same as a 9V 1A? Think I'm new at this?
gpsmikey:
"Nothing is running on batteries" but then you say "If I unplug the iPad and let it run on battery I get the same noisy results" so which is it?
Sorry... in the completed project both the arduino and iPad will need to be plugged in and running 27X7. For testing I've removed the iPad power to see if that made any difference which it has not.
Tourless:
Ok so I'm digging around for a different, comparable power supply... to that end I need to know what my limits are... I don't want to fry my board. Will a 9V 1500mA work for me? what about a 9V 300mA? 9V 700mA? Is a 9V 1000mA the same as a 9V 1A? Think I'm new at this?
Tourless:
Ok so I'm digging around for a different, comparable power supply... to that end I need to know what my limits are... I don't want to fry my board. Will a 9V 1500mA work for me? what about a 9V 300mA? 9V 700mA? Is a 9V 1000mA the same as a 9V 1A? Think I'm new at this?
You need to have a supply capable of supplying the needed current. YOU need to determine how much current your project requires and work from there. My preference is to typically use a supply rated at about twice the current my project requires so I am not pushing the supply limits. The current rating of the supply is what it is rated to supply (up to that amount). The actual current used will depend on what your project requires - if it only needs 200ma, then that is all it will take from a 800ma supply.
After trying a few different power supplies that fell within my range I noticed no difference. So out of curiosity I added an audio splitter and a speaker to test. Outwardly, there is no noise on the speaker and the tone comes through loud and clear. I did notice that my 'noise' was completely eliminated when using the splitter. When I remove the splitter and test with a few different m2m 3.5mm cables I get the same noisy results but the minute I reconnect with the splitter I'm perfect. In the pics (attached) you can see the audio wave looks noisy then not, depending on the splitter. So now I'm thinking it has something to do with the 3.5 stereo jack I'm using... should be mono? wired incorrectly? I'm at a little bit of a loss... any suggestions?
CrossRoads:
First pic looks like maybe missing the Gnd connection?
Interesting... I've linked the specific jack I'm using (below) and I have two wires running to it. Blue from arduino pin 11 to jack pin 5, and a grey wire from pin 2 on the jack to ground on the arduino. Me thinks my grey wire needs to be from pin 1 on the jack?
Well I tested my theory and it works! Turns out I was not properly grounded. I guess the moral of this story is I have to get better, much better, at reading schematics! A big thank you to everyone who got involved here, I really appreciate all the assistance. It's the great people like all of you that make a huge difference to those of us who are struggling along the way. Thanks for helping to expand my mind!
Ah, glad you got it solved. Missing grounds can cause some of the strangest things to happen (used to run into that with logic gates - like a quad two input nand gate (TTL). If you forgot to hook up Vcc (back in the days of wire wrap etc.) and any of the inputs was high, then the other 3 gates worked correctly since the high input was supplying power - you could spend quite a while chasing intermittent logic gates that way.)