6 Replies Latest reply on Mar 26, 2019 7:17 AM by vojec_2946236

    Control analog path from code.


      Dear Cypress community,


      is it possible to control an analog path in PSoC Creator 4.2 SP0 through some digital object?



      Currently I am dimming an LED by turning LED_OPAMP object on/off by LED_OPAMP_Start(); and LED_OPAMP_Stop(); (It's some kind of PWM)


      Is there a way to disable the analog output as if the opamp would be disabled by LED_OPAMP_Stop();


      Thank you.

        • 1. Re: Control analog path from code.


          it is unusual to run an LED from Opamp. Typically digital PWM output is used for that purpose. Is there any special need for using Opamp here? From schematic it looks like a Comparator (with digital output) is more appropriate here, and digital output pin can be enabled/disabled using hardware line or simply use the AND gate.

          • 2. Re: Control analog path from code.

            Hi, thank you for your reply, but it should be like it is, because we removed hw opamp and put it into psoc and its output is controling a transistor which controls power to LEDs.

            So the output needs to be analog.

            • 3. Re: Control analog path from code.


              To make it more clear, can you please draft that section of the circuit (basically, what goes into Opamp and the output transistor and LED)?


              • 4. Re: Control analog path from code.

                If the output pin is the dedicate output pin of Opamp, then there have no analog switch between opamp output and pin internally, it is directly connection inside, so you have to use stop() api.


                BTW, Why you use a analog pin to drive a led, i don't understand.

                • 5. Re: Control analog path from code.



                  It appears you are trying to drive the LED to control its intensity.  However to control intensity as an analog signal you need to control the drive current.  Voltage control of intensity is very problematic. This is because the LED which is a diode, has a very narrow voltage range where LED is full OFF or full ON.  For a standard red LED, that is basically <1.5 for OFF and >2.4V for ON.    Due to temperature effects (LED heat-up) and manufacturing process variations, using Voltage control is difficult to keep a consistent intensity.   The intensity to voltage relationship is definitely not linear.


                  If you must use analog to drive to an LED, there is a simple circuit to drive the LED with a settable constant current.  Since there is a near-linear relationship between intensity and current, this is a much better control circuit.  It also can prevent overdriving the LED current to prevent LED destruction and thermal runaway effects.  If you're interested, I can provide one.


                  A much better way to control LED intensity that is commonly used is to drive the LED through a current limiting resistor using a digital PWM output.  It saves on precious internal PSoC resources.  By controlling the PWM duty cycle, the integrity can be controlled without a feedback loop from the LED circuit.  Turning OFF or ON the LED using this method, is simple.  Just _Start() or _Stop() the PWM component.


                  To answer your original question directly, there are potentially multiple ways to switch the LED output ON or OFF.

                  • As hinted by another user, Use LED_OPAMP_Stop() API call.
                  • Set the drive mode of LED_OPAMP_OUT to digital output low to turn OFF the LED using
                    • LED_OUTPUT_OUT_Write(0);   // set the pin digital output value to low (0).
                    • LED_OUTPUT_OUT_SetDriveMode(Pin_DM_STRONG);   // change pin from analog Hi-z to digital output strong.  With the digital out value set to 0, the output will be low.
                  • Set the drive mode of LED_OPAMP_OUT to analog Hi-z to turn ON the LED using
                    • LED_OUTPUT_OUT_SetDriveMode(Pin_DM_ALG_HIZ);   // change pin to analog Hi-z.

                  There are other more obscured ways of controlling the ON/OFF of the LED output.  They should be used only under special circumstances.



                  • 6. Re: Control analog path from code.

                    Thank you all for the answers. I don't know why but I know it has to be like this. The opamp is there to even out the negative effects on the current.


                    If there is no other option with this hw configuration to control the output of opamp by digital signal, then i will use the opamp start and stop api.

                    I just thought that maybe there is a better option than to turn the whole opamp on and off.