Using arduino to emulate a joystick or other analogue inputs

Hey so I feel like this should be a pretty simple operation and I was surprised I couldn't find any forums on it, so if there is another forum that answers this question please feel free to link it here. But basically I have a drone controller and I want to hook up a arduino to the joysticks to basically be able to record whatever inputs I make and then be able to replay them back, to basically be able to very roughly be able to program maneuvers.

The reason I feel like it should be pretty simple is because shouldn't I just be able to jump a wire from each of the outputs on the joysticks into a serial input on the arduino then record it and then serial write the same information back to the controller later when I want the drone to repeat the maneuver. Is there a reason that it wouldn't be this simple? And if it is this simple is there any tutorial on how to do it? Because I'm more of a hobbiest than a engineer/programmer.

No,
the use of a "serial wire" is not how joysticks work.
A joystick is an analogue device (in this context at least) and so to tap into what it is outputting you need to read the four analogue voltages it is producing, along with any digital outputs that might be important. You must read them frequently enough to get a good resolution, but not too frequent as it takes up a lot of memory.

You would then need to store this data into an SD card.

When you want to play back this data you can't just connect it to the joystick because the joystick is producing a voltage output too. You have to arrange (add into the circuit) a data select switch between the joystick and your transmitter so you can choose if the transmitter gets to see the signals from your Joystick or from the Arduino. The Arduino would have to play back these readings through four 10 bit D/A converters. Any digital signals recorded can just be sent using normal output pins to the data selector.

This is probably why you have not seen a project like this before, because it is highly dependent on you being able to make a modification to your transmitter to fit a data select chip. So it would be different for each different type of transmitter.

I would clarify - external D/A converters, since ordinary arduino (on AVR platform) do not have built-in DACs. Arduino analog outputs are pvm, they cannot fully emulate joystick signals

Alright, so let me make sure I understand the entire system. The analogue outputs from the joystick go into one of the analog pins of the arduino, the arduino then logs the analogue readings at a decent resolution onto an sd card. Then when I'd want to replay the saved data, I'd have to use either SPDT switches or multiplexers (like this one? shorturl.at/dgsx7) to switch from the voltages coming from the joystick to the outputs coming from the arduino through a 10 bit DAC (kinda like this one? shorturl.at/hrwBK). Is there anything I missed?

If not do you know how I could learn to save analogue inputs onto an sd card, and then later turn that stored information into a workable analog signal through a DAC? Like I said I really haven't found any examples online of people working on similar projects to me, and I feel like that's mainly due to me not knowing what to look up.

I'm actually having trouble finding a DAC that would fit my needs. Would a digital potentiometer work better? Both size wise and cost wise those seem to be better options, but I just don't know if they'd fit for my application

But not resolution wise.

You need to have the same resolution, that is 10 bits, to be able to reproduce the reading that you take from the joystick.

You missed that that link was for a analogue to digital converter not what you need which is a digital to analogue converter.

It is worrying that you have to ask such questions, it suggests to me you are not ready to do such a project. And you would be better getting a lot more practical experience first. But to answer the question.

Look at the examples that come with any SD card library. Just one command will write an int variable to an SD card.
Also just one command will read the value from an SD card, and another single command will write the value to a D/A. Depending on what D/A you choose there might be a line of bit shuffling to do in the line before.

It is not the mechanics of reading and writing that is the difficult part, it is the overall way the system should work that you have not thought through. Like what file name do you store joystick readings in, and how do you recall a file to play back, without distracting your attention from actually flying the drone. Do you want to incorporate some way of immediately regaining control if things are going wrong?

Then there is the problem that you are only recoding relative movements. You can't have a manoeuvre with a 40 foot drop in it if you are currently at 30 feet. How would you stop that from happening. This sort of thing is maybe why you have not found similar projects.

I really appreciate the help. I found a helpful video on using the sd card, and the video even had a portion where it saved the inputs of a potentiometer and replayed them on a servo. The thing I'm still having trouble with is the DAC. I really don't know what I'm looking at here. I understand what it does and why I need one, but picking out the part is a bit confusing to me.

Also why wouldn't a digital pot work? Won't the microcontroller on the drone only be reading the voltage coming from the pot? So as long as the resistance level is the same as the joystick pot how would the drone controller be able to tell the difference?

Also this project is definitely beyond my level of understanding but I feel like that's how I learn best. I'm sure I'll make plenty of mistakes but I won't be flying around other people and my drone has obstacle avoidance so when something does inevitably go wrong hopefully it doesn't end up too bad

Would a 12 bit DAC be ok as long as I adjusted the numbers gotten from the 10 bit ADC? Because this is the best DAC option I've found so far in terms of size and price

A digital pot has at best only 8 bits resolution. That means the numbers you can send to it are in the range of 0 to 255. Using the Arduino to read the voltage results in numbers in the range 0 to 1023, so for each joystick movement the numbers you get are 4 times more accurate than the numbers you will be able to play back into your transmitter.

Yes.
But that link is for an I2C D/A converter. The I2C interface is comparatively slow and the boards are not actually in stock.

I have used the MCP4921 12 bit D/A chip in the past, and a quick look shows that there are chips of this device available. These use the SPI interface and so they are a lot quicker. However by limiting your search to a breakout board containing a chip you are vastly restricting your choice. It is very easy to solder these things to strip board and make your own board.

I think I was assuming that your joystick had four signals from it when I mentioned the number of signals it would take. However on reflection, most joysticks only have two signals, an X and a Y voltage. In which case you only need to wire up and record two voltages. If this is the case you would only need two D/A converters in your setup.

Ok that all makes a lot more sense. Thank you for the DAC recommendation, those are the ones I decided to buy. Hopefully it didn't seem like you were doing this project for me, but I just wouldn't have known where to start without your help so thanks!

Ok but just a little more help to get you started with this. The MCP4921 looks in software like a two byte register. This diagram shows the upper byte where there are four bits of control data and the upper four bits of the sample, and the lower bytes that are just the 8 least significant bits of your sample. The logical and & as well as the shift >> operations to apply to the sample value "v" are also shown on the diagram. I have a more complete diagram that allows you to use this as an 8 bit D/A as well because that is more complex than just using 12 bit values, but then you don't need that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.