Reading 0 or 1 with STM32 from intercom melody - Page 1
Forum section: electronics / beginners

author: symbianas
author_rank: Regular Contributor
timestamp: February 03, 2025, 06:50:35 pm
content: Hi, I would like to read 0 or 1 from my intercom system, but its not a constant signal, but a melody. I found schematic how someone did this but they do not specify C1 value:I am currently using C1 as Ceramic capacitor 103, but for some reason it does not work. Any ideas if this is a correct capacitor?

author: ledtester
author_rank: Super Contributor
timestamp: February 03, 2025, 07:20:21 pm
content: Can you share the link to the other person's solution?I would expect the circuit to look something like what's shown in this post:https://forum.arduino.cc/t/analog-music-input-detection/682942/3

author: symbianas
author_rank: Regular Contributor
timestamp: February 03, 2025, 08:00:58 pm
content: Quote from: ledtester on February 03, 2025, 07:20:21 pmhttps://www.edaboard.com/threads/intercom-opened-by-morse-code.248580/Input:When the button on intercom is pressed, the CA line charges a small capacitor C1. Zener diode limits the voltage to 5V (the melody has a greater amplitude). The capacitor provides the exchange of modulated melody to logic 1 or 0.Melody voltage seems to reach 12V

author: ledtester
author_rank: Super Contributor
timestamp: February 03, 2025, 11:47:24 pm
content: Just checking... are you connecting the ground of your microcontroller to the intercom?Note that D1 is a 5V Zener diode. Use a 3V Zener if you are running at 3.3V.Can you provide a scope trace of the voltage at the MCU pin?

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 04, 2025, 07:36:31 am
content: The original schematic uses a 5V AVR based MCU, while you are using a 3.3V ARM based STM32 MCU, and even though they have 5V tolerant pins, it is indeed better to use a 3V zener diode, like ledtester wrote.In the original design it is not clear what the call signal actually is, and the shortest time it checks on is below 0.3 seconds, so the capacitor can't be to big in this case. The design uses a low pass filter but also a voltage divider and voltage limiter, which makes it harder to calculate on.To make it more like an envelope follower I would add a diode in series with the call line, and make it more responsive by lowering the value of R5. This way the capacitor charges faster to the zener voltage when the input becomes active and only discharges over R7 depending on if it is much lower then the MCU input resistance.

author: symbianas
author_rank: Regular Contributor
timestamp: February 04, 2025, 07:42:01 am
content: Its 5V tolerant pin so 5V should be fine.Connecting 5V to the pin from MCU triggers input to high (WORKS)Connecting regular 12V power source for testing does not work and does not trigger MCU input to high (DOES NOT WORK)Is it because of external power source? MCU cant read external voltage as input or something?

author: ledtester
author_rank: Super Contributor
timestamp: February 04, 2025, 08:34:29 am
content: One troubleshooting method is to come up with the simplest circuit which still doesn't work.So... remove the MCU, the Zener and the cap leaving only a voltage divider. You said the input was as 12V signal -- how did you measure that? Do you have a scope? Leaving just the voltage divider the input signal should be reduced to about a 8V signal.If you see the 8V signal I would connect the midpoint of the divider to a GPIO pin and see if the MCU can detect the signal. Even though the voltage will rise above 5V the resistors R5 and R7 are so large this shouldn't damage the GPIO pin.Try different GPIO pins.Maybe try swapping R5 and R7 -- the signal at the divider's midpoint should then be about a 2V signal.

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 04, 2025, 09:15:09 am
content: What is the source of the external signal? Is it AC or DC?with the 220K resistor and the 5V zener it is only ~32uA so should not be a problem.The difference between the two test scenarios is R5. Try it with connecting the 5V to the other end of R5 and see if it still works. It should give ~3.4V on the input and that is more then enough to be seen as a high.

author: symbianas
author_rank: Regular Contributor
timestamp: February 04, 2025, 09:25:04 am
content: So I have this voltage divider with 7.5k ohm and 30k ohm resistors:https://lastminuteengineers.com/voltage-sensor-arduino-tutorial/I have connected it to MCU (PA11 pin) and 12V power source and it does trigger GPIO input pin to high, however when trying to trigger input again after removing 12V and connecting it again there is like ~1second delay after which it can detect second 12V pulse. If I test 5V pulse from MCU (connecting to same pin without voltage divider) I can trigger it many times without any delays.

author: symbianas
author_rank: Regular Contributor
timestamp: February 04, 2025, 09:36:08 am
content: Quote from: pcprogrammer on February 04, 2025, 09:15:09 amCurrently testing with regular constant power of 12V DC. Intercom signal should be sound melody which when measured with multi-meter is 12V DC average.Quote from: pcprogrammer on February 04, 2025, 09:15:09 amNo, connecting 5V to the other end of R5 does not work either (does not trigger input to high). Input seems to be getting only 1.3V.In theory it should be without diode and capacitor.12 * (470_000 / (470_000 + 220_000)) = 8.173913043478261VWhich is too big?

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 04, 2025, 09:59:48 am
content: Quote from: symbianas on February 04, 2025, 09:36:08 amThat means that either your R5 or R7 are not what they should be. So make sure the values are correct and the resistors are working properly.Quote from: symbianas on February 04, 2025, 09:25:04 amDid not calculate, but sounds like over voltage problem on the input causing the internal circuitry to lock up until some capacitance has been drained.

author: ozcar
author_rank: Frequent Contributor
timestamp: February 04, 2025, 10:24:37 am
content: Any particular reason why you chose to use PA11? Using that means you can't use USB at the same time, say to print debugging information.Do you have the pull-down (or pull-up for that matter) enabled? The datasheet says those are typically 40k, but can be as low as 30k.

author: symbianas
author_rank: Regular Contributor
timestamp: February 04, 2025, 11:53:32 am
content: Quote from: ozcar on February 04, 2025, 10:24:37 amNo particular reason only that PA8 and above pins 5V tolerant. I can switch to any PA8/PA9/PA10/PA11/PA12/PA15. Pin is configured as GPIO Input with pull-down option to get high input if pin goes high.Quote from: pcprogrammer on February 04, 2025, 09:59:48 amvoltage after voltage divider seems to be 2.4V... I guess I need to go back ADC and measure analog signal? But how will I remove melody signal fluctuations? Simply by introducing capacitor?

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 04, 2025, 12:14:15 pm
content: QuoteSee my first post about adding a diode. Search for envelope follower on the net to see how it works.The diode makes sure the capacitor is not discharged by the call signal when it goes to zero or even becomes negative.

author: symbianas
author_rank: Regular Contributor
timestamp: February 04, 2025, 02:09:16 pm
content: Quote from: pcprogrammer on February 04, 2025, 09:59:48 amVoltage divider module:12 * (7_500 / (7_500 + 30_000)) = 2.4VWhy would 2.4V cause "over voltage problem"?

author: xvr
author_rank: Frequent Contributor
timestamp: February 04, 2025, 03:53:27 pm
content: QuoteTurn off pull-down. With R5 it forms divider about 6:1 to 10:1. Input 12V will be bring down to 2-1.2V (this is too low)

author: ozcar
author_rank: Frequent Contributor
timestamp: February 04, 2025, 05:01:18 pm
content: Quote from: symbianas on February 04, 2025, 11:53:32 amThe pull-down can be used to make sure that the pin reads low when it is left floating (as it would be if say all you had connected to it was a switch to +3.3V, and that switch was open). Similarly, if the switch connected to ground instead, you could enable the pull-up to ensure you read high when the switch is open.I've lost track of all the things you said you tried, so I'm not sure that the presence of the pull-down explains all of them, but as xvr says, it would have a large effect with the resistance values you showed in your first post.

author: symbianas
author_rank: Regular Contributor
timestamp: February 04, 2025, 06:32:05 pm
content: Disabling pulldown on GPIO pin does help and voltage divider triggers it to high, but it never goes back to low. So I must use in pulldown mode?

author: xvr
author_rank: Frequent Contributor
timestamp: February 04, 2025, 06:55:13 pm
content: Take a look what you have on CALL input. I suspect that you have some non zero voltage on it in any state.

author: symbianas
author_rank: Regular Contributor
timestamp: February 04, 2025, 08:00:46 pm
content: Quote from: xvr on February 04, 2025, 06:55:13 pmThis is how CALL signal looks like. So it will be able to turn to low on its own?(I guess I have made a mistake with testing environment "12V power supply" and removing it instead of pulling it to ground to imitate CALL which would pull down on its own?)VIDEOhttps://streamable.com/q7vkln(It seems to be DC?)When calling:Vavg = ~6VVrms = ~0V - ~12V (jumping)Vpp = ~11VNot calling:Vavg = ~0.7VVrms = ~0.7VVpp = ~0.3V

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 04, 2025, 08:10:10 pm
content: Quote from: symbianas on February 04, 2025, 02:09:16 pmNot the 2.4V itself. But there may be voltage spikes when switching from off to on and these can be a problem.When dealing with more or less unknown voltage sources, I would use an optocoupler to avoid any problems. This way the signal level on the MCU can be in the range of what it likes, 0 to 3.3V in this case. Forget about the 5V tolerant, it will make things last longer when you operate in the safe region. For 5V tolerant there is an internal circuit that can cause the 3.3V supply to be raised when to much current needs to be processed. Depends on how low the internal resistance of the 3.3V supply is.

author: ozcar
author_rank: Frequent Contributor
timestamp: February 04, 2025, 11:20:21 pm
content: Quote from: symbianas on February 04, 2025, 08:00:46 pmIf you just disconnected the 12V, the capacitor should still discharge through R7. If you used a 10000pF capacitor, the RC is 4.7 seconds(*), so you could notice delay, but if it is still high say a minute later there is something else going on there. What does the signal on the GPIO itself look like?10000pF would be too large if you were trying to read morse code as per the source of that circuit, but I get the impression you don't need to do that, just know if the "melody" is playing or not?(*) Edit - not even close, see subsequent posts.But that means it should revert to reading as zero much quicker when the 12V is disconnected.

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 05, 2025, 06:37:24 am
content: Quote from: ozcar on February 04, 2025, 11:20:21 pmYour calculation is way off. Based on only R7 being 470K and a capacitor of 10000pF which is 10nF or 0.00000001F results in a time constant of 0.0047 seconds.https://www.allaboutcircuits.com/tools/resistor-capacitor-time-constant-calculator/But to calculate the actual on time in this circuit, way more parameters need to be considered. One needs to calculate the actual load resistance first. This is based on a couple of parallel resistances to ground either directly or via the supply rails. Then one needs to calculate the duration the circuit takes to go from the limited max voltage of 5V down to the level where the MCU decides it is low, which might be something like 1.5V or 0.8V.But that is complicating things not needed here. My earlier advice to look into an envelope follower should help the OP solve this problem.

author: ozcar
author_rank: Frequent Contributor
timestamp: February 05, 2025, 12:47:08 pm
content: Quote from: pcprogrammer on February 05, 2025, 06:37:24 amOutch, what's a factor of 1000 amongst friends? You are of course correct, I was somehow thinking in pF, but calculating in nF!

author: symbianas
author_rank: Regular Contributor
timestamp: February 05, 2025, 09:48:16 pm
content: So the original circuit in my first post works just fine (with intercom)!The issues were:* at first I was testing without connecting intercom GND to the STM32 GND* then decided to make test setup with 12V, but removing it is not the same as pulling it down to 0V* tried messing around with code, introduced pulldown...and all things just went south and non of the solutions I though of were working...So "no pulldown no pullup" seems to be the main reason. Also connecting intercom GND to STM32 GND. Making a better test setup...However the question with capacitor remains. Currently I am using 103 capacitor which seems to work ok, but I just took random capacitor as this circuit does not specify capacitor value.Quote from: ozcar on February 04, 2025, 11:20:21 pmIt seems that it stays high forever, thats why I thought pulldown is a must.Quote from: ozcar on February 04, 2025, 11:20:21 pmYes, the idea is to read morse code. It seems that I am able to read morse code! Just need to adjust timing of dot and dash.

author: ozcar
author_rank: Frequent Contributor
timestamp: February 06, 2025, 12:06:12 am
content: Quote from: symbianas on February 05, 2025, 09:48:16 pmAs pcprogrammer pointed out, my previous attempt to come up with an estimate figure for the timing was not even close. The page you pointed to mentioned "dot" timing of less than 300ms, and "dash" 300ms to 1s - unless the tones that make up the dots and dashes are very low frequency, the capacitor you are using could be OK. Did you look at the signal on the GPIO?Years ago when I did something with morse code, I used timing of dash = 3*dot, and inter-character gap = 5*dot, but I'm sure you should be able to turn up some official or at least accepted standard for that. (I was doing it in about 20BG, as in 20 years before Google was invented.)

author: symbianas
author_rank: Regular Contributor
timestamp: February 06, 2025, 03:57:41 pm
content: This is how input signal looks likeVIDEO:https://streamable.com/9y25clIs it good enough? Or should be complete straight and need to increase capacitor?

author: ozcar
author_rank: Frequent Contributor
timestamp: February 06, 2025, 08:08:27 pm
content: To me it looks OK for your purposes.But it is difficult to see from that video. Is that how you try to see what is happening (DSO in AUTO RUN mode, and take a video)? I'm guessing that the triggering available to you is not very sophisticated, but a few single-shot captures would be easier to look at.

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 07, 2025, 06:47:08 am
content: Quote from: symbianas on February 06, 2025, 03:57:41 pmThe ripple can be reduced with the use of a larger capacitor, but it will also lengthen the total on time. The ripple is for a larger part caused by the discharging over R5 when the input signal is low. Add a diode as I suggested earlier and it will reduce the ripple, but also lengthen the total on time. So it is a case of finding middle ground.But as long as the low level in the ripple is above the MCU low threshold it does not matter that it is there.