LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Controlling external hardware with the 3DS+SmileBASIC

Root / General / [.]

amihartCreated:
SmileBASIC could potentially be used to interact with and control external hardware, such as motors, character displays, and more. Headphone jacks don't output very useful digital signals. They are incredibly low voltage, low current, and analog, purely meant for audio. But could we turn that into a digital signal we could work with, power LEDs, and potentially use to control external hardware? Alternating current looks like a sine wave as the voltage rises and falls over time. Sound waves are created in speakers by alternating voltage, so if you play a sine wave, you producing alternating current. This means you can simply build hardware to convert alternating current to direct current and you have a digital signal. The headphone connector is a TRS connector, standing for "tip, ring, sleeve". These are the pins on the connector. The "sleeve" is ground, and the "tip" and the "ring" carry the audio data. So if you convert the two analog audio signals into digital signals, you will have two pins you can write to. And thus, our two pins for controlling our LEDs. AC alternates between negative and positive voltage over time in a sine wave. DC is simply a single positive stable voltage. So how can we convert AC to DC? Well, the typical way to do it is by using some diodes and capacitors. You see, if you run the alternating current through a diode, the current can only flow forwards, so you will cut off the negative part of the sine wave. What you are left with is a bunch of positive humps with gaps between them where the negative humps were cut off. This is a process known as "rectification". A full bridge rectifier uses four diodes and an arrangement such that no matter which direction the current is flowing, forwards or backwards, the diodes always direct it towards a singular point. Rather than removing the negative part of the sine wave, it flips it to positive. This is known as "full wave rectification". DC is a steady, positive voltage. By rectifying the AC by passing it through some diodes, we don't get a steady, positive voltage. We get a bunch of positive humps. How can we convert this into a steady voltage? The trick is capacitors. When the voltage is rising, the capacitor charges. While the voltage is falling, the capacitor discharges its stored voltage, which keeps the voltage up. The capacitor should have just enough capacitance so that it can supply volts between humps, this will keep the voltage stable. Everything I've explained doesn't really work, though. This is how you would usually do it. If you were building an AC to DC converter to convert your home outlet electricity to something you can use in your computer, this is how you would do it. Albeit, you'd probably throw in a line voltage regulator if you were making a high quality one. But the problem with audio is that it is not 120 volt 12 amps alternating current like you'd get out of the wall outlet. Barely any energy is used for headphone jacks. In fact, I've measured it, and at max volume, the most volts I can get out of the 3DS's headphone hack is 0.11 volts with PCMSTREAM (I managed 0.17 volts with WAVESETA but we will be sticking to PCMSTREAM for now). Here's a 5 kilohertz sine wave I generated using PCMSTREAM: This becomes a huge problem when you realize that diodes have a drop-off voltage of about ~0.7 volts. This means that you lose voltage by using a diode, and the amount you lose is greater than the amount of volts you even have. In fact, I passed the signal through a diode just to see what would happen, and it turns into this mess... Not only did it not rectify the signal, it's somehow even more negative! Also, the voltage drop off made it now 5 times smaller. So what can we do about this if we can't use regular diodes? The solution is super diodes, also known as "precision rectifiers". Super diodes are a bit more clever. They don't simply block current from flowing in one direction. They use a chip called an op amp that can subtract one analog signal from another, and they subtract out the negative part of the sine wave. The chips have to be powered from an external source. In this case, we'll use 5 volt microUSB. Op amps require diodes to use, but the voltage drop from the diode does not affect the voltage you are trying to rectify. Instead, it affects the external source that is powering the op amp (our microUSB source). Op amps are also amplifiers, so you can amplify the signal at the same time you are rectifying it. All you have to do is use the right resistors. One resister over another is the gain from a super diode. USB is 5 volts. I am trying to amplify a 0.11 signal. So 5 divided by 0.11 is 45.45. I want two resisters such that they also divide to a number close to this, so my gain will amplify the signal up to 5 volts. (You don't want to amplify the signal more than you need to, as amplifying it too much will also amplify noise and make the signal horrible). Luckily, I happened to have two resisters, 1 megaohm and 22 kiloohm, which exactly give me what I am looking for. Divide the two and you get 45.45. Here is my super diode. You can find the diagram on how to build this on Wikipedia. Notice the beautifully rectified output we get. Note that none of the signal is negative, I just scrolled up a bit so the waveform would fit on the screen. It is all positive. But why is not 5 volts? That's because diodes have a voltage drop between 0.3 - 0.7 volts. Yes, the voltage drop did not affect my low voltage signal, but it did affect my amplified signal. In fact, it affects it twice, because you need two diodes for the super diode. So now we are below 4 volts. We could then use a capacitor to stabilize this humpy signal. I connect up an LED such that we have a complete circuit around a capacitor, and I also connect the output of super diode into the capacitor's Vcc and connect its ground to the shared ground. Now if I measure the voltage at the capacitor's Vcc and ground, notice what we get. We now have a stable 2.63 volts. The capacitor has a voltage drop in of itself, so we lose even more volts at this step. But with this stable voltage, we can power our LEDs reliably, and we can control this stable voltage from SmileBASIC just by playing or not playing our sine wave. A problem here is that we are left with very little voltage. Less than 3 volts is enough for some simple things, but it's probably best to do something like this using a 9 volt battery rather than USB so we can have much more volts to work with. You can't amplify much with just 5 volts. Amplifiers saturate at the voltage of your amplifier, meaning, even if I setup my resistors so that my gain was thousands of times, it'd saturate at 5 volts and couldn't amplify any greater than that. So if we want a stronger signal, we just need to up the voltage. But, yeah, this is the general concept. It shouldn't be too hard to use this to start experimenting with controlling external hardware with SmileBASIC and the 3DS. Here's screenshots of the video of this working in case you don't want to watch the video itself... Here's a video version of this. This is very cheap and easy to do. The op amp chips are also nice because they have two op amps on them, so you can rectify and amplify both audio channels with a single chip, making it capable of controlling two LEDs. It really is just one chip, two capacitors, four resistors, some wire, a headphone jack breakout board, and something to power the board with.