This is my first post, although been playing with Arduino for a few months now with IoT projects.
I am not sure where I should post this question so please advise if this is the wrong section.
I want to use an ADXL345 3 axis accelerometer to measure the side to side acceleration of a trailer then set an output to high once a thresh hold is reached, then set to low again once it falls below another thresh hold or after a set time. (or an alternative method if someone has a better idea)
This output will be attached to a relay that will connect to the electric brake to bring the caravan back under control.
There are commercial systems out there to do this but they cost up to $2000!
As I am not an experienced programmer I am a little lost on where to start with this?
Any advice, help or direction would be welcome, thank you.
Ideally, there should be no requirement to calibrate or set the device up once installed, ie, it should detect the direction of normal travel and self-calibrate to detect side to side motion.
Start by buying the accelerometer, hook it up to an Arduino, and learn to understand what the accelerometer is telling you. This is NOT trivial, as most people have serious misconceptions about the meaning of acceleration.
For example, your specification that "it should detect the direction of normal travel and self-calibrate to detect side to side motion" is simply impossible. An accelerometer cannot detect direction of motion, only direction and magnitude of acceleration.
Every bump in the road is an acceleration that will be measured, and then, there is good old gravity, which is always accelerating things, including your accelerometer, down.
Furthermore, only you (with the help of the program you will write for your trusty Arduino) can determine what constitutes "side to side motion" and set a threshold for when this motion becomes a problem. Note that if you mount the accelerometer such that the X axis is pointing in the side direction, oscillating X accelerations will indicate sway.
Adafruit and others have tutorials on how to set up and use this accelerometer with Arduino.
I have been programming for about 30 years now, and there's no way I'd hook up something that I wrote to the brakes of a motor vehicle travelling on a public road. And it's almost certainly illegal.
An alarm, however, is probably do-able.
Go research "fourier transform". You need to decompose the motion of the caravan into its frequencies, and fire an alarm when those frequencies typical of "sway" start to get out of hand.
There are ways to stop sway once it has started, but these will not always work if road conditions and surroundings are working against the driver.
a) If you are going down a hill, manually apply the electric trailer brakes, this will cause the caravan/trailer to slow the towing vehicle down and almost instantly reduce the sway.
b) If you are going up a hill, you can accelerate slightly until the sway stops, then slow down and drive at a lower speed.
c) If you are driving on flat roads slow down gradually, without applying the tow vehicle brakes.
So brakes are not always going to stop swaying.
I agree with PaulMurrayCbr, an alarm would be a brilliant project.
My 5th wheel trailer hitch in my truck uses strain gauges to recognize when I am applying truck brakes and the controller then applies appropriate pulsed electric current to the trailer brakes. Works very well.
The OP may want to consider strain gauges to his project. I'm not sure where he intended to place his accelerometer or what kind of vehicle he is considering. I also think he is referring to yawing, not swaying. Braking may limit yawing, but not swaying. Braking would only change the direction of swaying by a small amount.