Help understanding a circuit - Page 1
Forum section: electronics / beginners

author: terriea
author_rank: Contributor
timestamp: July 17, 2010, 10:42:52 pm
content: Hi folks,Yet another newbie question.I bought a cheap USB Joystick that never worked, and I decided to keep the case and replace its internals with a Microchip based circuit to make my own USB joystick. The thing is this Joystock have a board with buttons and LED for its frontface. I need to hook up the Microchip to it, and I have to figure how it works to do it properly. I made a quickNdirty diagram of the board which is very simple:There are five buttons, four LEDs, all driven by eight pins. The LED on the left and the two buttons on the right are quite simple, but what puzzle me are the three buttons and LEDs in the middle. They share the same pin for button sensing and LED switching. How is it possible to set the pin voltage to switch on/off the LED AND sense the button on the same wire ?Thanks for your help

author: Zero999
author_rank: Super Contributor
timestamp: July 17, 2010, 11:03:06 pm
content: The circuit doesn't have a power supply, which is positive, negative, 0V?Assuming the top rail is negative and the bottom rail is 0V, otherwise it wouldn't work.When the switches are open, the voltage on the output will be equal to 0V. When the switches are closed the output will be lower (remember it's a negative supply).For example, suppose it runs off -15V and the LEDs have a voltage drop of 2V.The current through an LED when the switch is closed:I = (V-Vf)/R = (-15-(-2))/(1000+1000) = (-15+2)/2000 = -13/2000 = -0.0065 = -6.5mAThe voltage across the top resistor is equal to:V = RI = 1000*-0.0065 = -6.5VTherefore the voltage at the input is equal to the supply voltage minus the LED's forward voltage:V = Vs-Vf = -15-(-6.5) = -15+6.5 = -8.5VThe negative numbers are because the voltage is negative.I probably could've saved a bit of time by using the potential divider calculation, it's just easier for me to work it out in my head by doing it the long way.

author: RayJones
author_rank: Frequent Contributor
timestamp: July 17, 2010, 11:19:11 pm
content: We have to presume the top rail is ground and the lower is Vcc.The secret is the micro can, within a few instructions, set the pin as an input instead of an output, and determine whether the switches are closed. You would need weak pullups at the micro to sense the 1K being grounded by the switches.ie. if the micro's pull up was 10k the resultant voltage could be a marginal logic 0. If the pull up was larger, the voltage would be lower and a more reliable 0 could be ensured.Actually on second thoughts it is not that easy, the LED is still in circuit, and will contribute as a pull up too, albeit the drop across the LED will set a lower source voltage.You would have to check the input levels for a valid logic zero to see if your logic zero would be reliable.If LED is ostensibly on, the brief moment the micro pin becomes an input would be unobservable to the human eye.Won't the LED light when a switch is held on?NO, if the micro is sending a logic high out for the LED off state, the LED will remain off as the 1K will not be strong enough to overcome / drag the LED's cathode low enough.This is actually common practice for front panel displays sharing a pin's function in such a way, and especially handy if the micro's pins are in short supply, but you have to be careful with the logic zero levels coming back to the micro.

author: terriea
author_rank: Contributor
timestamp: July 17, 2010, 11:48:14 pm
content: Thanks very much for the explaination. That's exactly what I was looking for. So simple I didn't thought of it. Switching the pin to input just for reading and put it back to output. I'll chek to the levels and try not to blow everything.

author: RayJones
author_rank: Frequent Contributor
timestamp: July 18, 2010, 12:11:58 am
content: You will still need to take care with the usual de-bouncing of the switches.All that really changes in software is you need to add the port direction manipulation to your switch reading routine, otherwise it works the same.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 18, 2010, 02:24:42 am
content: i'll prefer position sensitive joystick where the software will know if we just tilt the joystick a little bit or all the way, usage of potentiometer and chip ADC feature is the way to do. do you mind to post the joystick picture here? i'm not sure i understand the schematic above. probably u meant the diode symbols are actually the LEDs?

author: terriea
author_rank: Contributor
timestamp: July 18, 2010, 04:21:55 am
content: RayJones>I saw a code example of software debouncing. Didn't really dig into it, but seems the principe is to check if the value read from the pin is stable for a certain delay before considering the input have actually this value. Is there a rule of thumb to calibrate the delay of the debounce ?shafri>This Joystic is similar than the ones fond on arcade machines. The directions and buttons are all switches. Much more suitable for action games requiring fast/accurate actions. Positions joytick are far better for games requiring steady accurate action like flight simulators. I bet that's more your type of gamesFor more precisions about the scematic, it is only a small part of the joystick. More precisely the joystick is originally composed of two boards. One main board with the main controls (directions, actions buttons, ...) and the chip. A second front-panel board with a "status" display (LEDs) and some controls buttons (start/select/etc..). I can replace the main board because the buttons are separate components attached to the case, not to the board. But for the front-panel board, the buttons are just contacts on the board itself, the LEDs are smt, and the board if shaped to fit exactly in the case with buttons and LED position matching the layout of the case. In a word it would be a nightmare to make another front-panel board. The schematic represent only this front-panel board, the 8 endpoints on the left represents the 8 wires coming out of the board that I have to plug to my microchip (which will replace the defective main board).And yes the diodes represents the LEDs. again, It's a quick n dirty schematic. very quick, very dirty.

author: RayJones
author_rank: Frequent Contributor
timestamp: July 18, 2010, 04:49:02 am
content: Quote from: terriea on July 18, 2010, 04:21:55 amYes, every switch contact has bounce when the contact is opened/closed.The usual approach is to initially sense a change, then wait 10ms or so before accepting the switch state in software.If you don't do this your software will more likely than not see multiple presses for one nominal physical operation of the switch.If you're lucky you have a manufacturers data sheet that will specify the bounce duration, or next best use a DSO and capture the input a few times and see how long it lasts for.A good rule of thumb is to then at least double the delay time to ensure long term reliability.

author: Zero999
author_rank: Super Contributor
timestamp: July 19, 2010, 08:10:50 am
content: Quote from: shafri on July 18, 2010, 02:24:42 amIf I remember rightly, old joysticks were completely analogue and used to connect to a special connector on the sound card which used an ADC.A modern USB joystick with probably have an MCU and an ADC built-in.

author: RayJones
author_rank: Frequent Contributor
timestamp: July 19, 2010, 08:39:54 am
content: Actually if I remember rightly, the "ADC" was merely a multivibrator who's frequency varied according to the joystick pot's position.The frequency was fed into a gated counter, the number of pulses counted over known period determined the position.So not an ADC as you would normally consider / use, but perhaps useful to keep in mind for other designs.