Tech Talk with Gus 023 - Analog Inputs

On today’s episode we’ll be talking about analog inputs, how they work, and why they are used.

1 Like


using System;
using System.Threading;
using GHIElectronics.TinyCLR.Pins;
using GHIElectronics.TinyCLR.Devices.Adc;
using GHIElectronics.TinyCLR.Devices.Pwm;
 
namespace Analog
{
    public class Program
    {
        public static void Main()
        {
            AdcController ADC = AdcController.GetDefault();
            AdcChannel A0 = ADC.OpenChannel(FEZPandaIII.AdcChannel.A0);

            PwmController PWM = PwmController.GetDefault();
            PwmPin Buzz = PWM.OpenPin(FEZPandaIII.PwmPin.D5);

            double lastAnalog = A0.ReadRatio();

            while (true)
            {
                double delta = Math.Abs(lastAnalog - A0.ReadRatio());

                if (delta > 0.01)
                {
                    lastAnalog = A0.ReadRatio();
                    Buzz.Stop();
                    PWM.SetDesiredFrequency((lastAnalog * 3000) + 300);
                    Buzz.Start();
                    Buzz.SetActiveDutyCyclePercentage(0.5);

                    //System.Diagnostics.Debug.WriteLine("A0 " + (int)(A0.ReadRatio() * 100));
                }
                Thread.Sleep(50);
            }
        }
    }
}
 

1 Like

Thanks, good for us noobs. Along those lines I have for quite some time wondered how to calculate the optimal resistor sizes for a voltage divider when you have 3.3V input, the photoresistor you have varies from 350 ohms to 35K ohms and you want 0-1.0V into your ADC…?? Any tricks?

1V is about third 3.3V so your resistor ratio will be 1/3. Something about 700 ohm -> 350/(350+700). It will not be exactly 0V to 1V but for the sake of your example it will work perfectly fine.

Or more precisely, wouldn’t something closer to 152 Ohm’s be ideal (152 = 350 * 1/(3.3/1 - 1) ) ?

@ ianlee74 -

And use 1% precision resistors. Not sure TC’s come in values less than 1 K.

1 Like

Yea, yea… :wink: Just thought I’d show the maths to @ njbuch.

Depending on where you measure, across the resistor or the photoresistor, you can use 150 or 700. The 700 is better because you are using less power. Also, you would use a common value resistor and scale in software. For example, 470 is very common but 500 is not common.

1 Like

@ Gus - @ ianlee74 - thanks for input, I think we need this drawing before you continue…

I follow you mostly, but Gus started on some power considerations, can you explain…

EDIT: In this case R1 is the photoresistor…

@ cyberh0me - Good point, does it then make sense to place a resistor in serial with the photoresistor to bump the total resistance?