I have been playing with my Arducopter for a while now. Really cool stuff! It's time for a little contribution myself.
But first a really a big thank you to everybody working on this project and especially Jason for all the cool (and very stable) code he has been contributing!
I had hooked up the Battery Monitoring as described in the WIKI. Works like a charm, however beyond a message in the GCS it was hard to notice when the Voltage (or current, if that is hooked up) reached a critical level.
RTL sounds great, but in most cases it'll be good to get an audible signal when the batteries run low. In fact, I killed one LiPO just having my copter sitting on the desk and forgetting to disconnect the battery. The ACM draws quite a bit of current and low-discharged the LiPO... 20 more bucks down the drain.
So I had a look around in my electronic shop and found a little Piezo buzzer. It runs on 3-6V and only draws about 30mA which is not too much to be driven straight from one of the ACM port pins. It's a YMD1205. It only costs 30 cents - much cheaper than a new LiPO :)
After looking at the schematics of the IMU a usable port was quickly found. The IMU has two "Expansion Ports". As I did not plan to use either for other stuff, they are perfect as the buzzer fits neatly between one of the "gnd" pins and the hole marked "AN6". It looks like it actually belongs there, doesn't it :)
Code was written very quickly as well in case you would like to integrate that into your project.
I wanted it to be configurable via the APM_Config.h file. Also I integrated a quickly audible beep on start-up to know if the Piezo Buzzer is working.
I tested the whole thing and it works like a charm. The nice side effect is, that the buzzer can be used from anywhere in the code for other audible signals via the piezo_on() and piezo_off() functions.
Let's see maybe I can get it to play a song before it takes off :)
APM_Config.h:
// Enables the Piezo buzzer code
#define PIEZO ENABLED
// Required if PIEZO is "ENABLED"
#define PIEZO_PIN AN6
// Optional to enable low Voltage alarm
#define PIEZO_LOW_VOLTAGE ENABLED
system.pde:void init_ardupilot():
#if PIEZO == 1
pinMode(PIEZO_PIN,OUTPUT);
piezo_beep();
#endif
sensors.pde:
#if BATTERY_EVENT == 1
if ( (battery_voltage < LOW_VOLTAGE) || (g.battery_monitoring == 4 && current_total > g.pack_capacity) ) {
low_battery_event();
#if PIEZO_LOW_VOLTAGE == 1
// Only Activate if a battery is connected to avoid alarm on USB only
if (battery_voltage1 > 1){
piezo_on();
}
else {
piezo_off();
}
#endif
}
else {
#if PIEZO_LOW_VOLTAGE == 1
piezo_off();
#endif
}
#endif
events.pde:
#if PIEZO == 1
void piezo_on()
{
digitalWrite(PIEZO_PIN,HIGH);
}
void piezo_off()
{
digitalWrite(PIEZO_PIN,LOW);
}
void piezo_beep()
{
// Note: This command should not be used in time sensitive loops
piezo_on();
delay(100);
piezo_off();
}
#endif
Only one small annoyance. It does beep if the ACM is powered by USB cable only. I am wondering if there is a way to detect the presence of the USB cable plugged in, in code, which would allow me to disable the buzzer in that condition. Any hints would be appreciated.
Have fun !
Ollie.
Comments
Oliver, does your piezo can work in "AN8<->AN15" output ports?
From Mikrokopter documentation: "Note: if you are going to install the new MK3 magnetometer on your MK then you should move the buzzer out of the board, far from the MK3 and connect it with wires. It would affect the magnetic field otherwise.".
Yes Rui, you should get yourself one of the piezo's that make a constant noise when you connect them to the 5V rail. Using a PWM pin would be a waste of processing power and pins unless you want your copter to play a tune ;)
Hello. I'm trying to get a buzzer on my arducopter too. I have the same problem... just clicks, even when connected directly to the 5V power rail. I went to make some tests with an arduino nano, and I get buzz sound if I connect the buzzer to a pwm pin and use analogWrite, however, if I use a digitalWrite I get only clicks when on and off. The pwm pins send pulses, so, do you think the buzzer you're using may have an oscilator integrated ? Just a thought, I'm not an expert. I then made another test, using a digital pin (not pwm), alternating between HIGH and LOW with a 2ms pause between and I managed to have a tone, wich can be changed when you change the delay time.
void setup (){
pinMode (5, OUTPUT);
}
void loop(){
digitalWrite (5,HIGH);
delay (2);
digitalWrite (5,LOW);
delay (2);
}
Let me know if I'm right. Users who are getting cliks may have piezos without oscillator and those who are having a tone, may be using some kind of buzzers with integrated oscillator.
Will do just to make sure on the Piezo works and will look forword to the code.
Jack
Hi Jack,
have been working on it Monday. Let me test it to ensure it does not interfere with anything and will upload it towards the end of the week.
Can you connect your Piezo to the 5V rail, just to make sure it beeps continuously when power is applied ?
Just to ensure you dont have a hardware issue or the piezo draws too much current.
Ollie.
Ollie
Any news on the updated code for this so Jason can commit it ?
Jack
Ollie:
Thanks for the info I did look at the code but I,m not good enough to make those changes. I will wait till you have time to make the changes and have Jason commit them.
Thanks Jack
Jack Dunkle
I'll made some changes to the code, as the code that Jason had integrated was many month old and the surrounding code had changed as well. I will update/improve the piezo code and get it merged into the code base probably around end of week after next.
The reason it klicks, is that it gets reset through the low battery loop if you would like to have a look yourself.
Ollie.