Javascript required
Skip to content Skip to sidebar Skip to footer

Raspberry Pi Read Ct Sensor Output Voltage on Gpio

In this tutorial, we will explore how the Raspberry Pi tin can exist used to read analog signal data accurately and the use of potentiometers in carrying out this great task. Let's get started!

PCBWay Ad

One thing to note about the Raspberry Pi is that it is unable to read analog signals directly. It tin can find digital signals like the push of a push or a unproblematic change in a digital sensor's land, but not the changes made by an analog sensor. Simply the good thing, in that location are two means to actually sort this problem out.

The first method is to use an ADC (Analog-to-Digital Converter) like the MCP3008 (for 8 channels of analog input) or ADS1115 (for 4 channels of analog input). The only divergence between them is that ADS1115 is ameliorate in precision compared to the MCP3008.

Fortunately, there's another way to read the analog signals without having to buy an ADC flake. This saves usa from the hassle of spending a lot on expensive chips. This method is known every bit "footstep-response." Using this methodology, nosotros meet how the circuit behaves during a change in state from loftier to low or low to high, besides known every bit 'step modify.' This is done by using the charging and discharging circuit controlled by a potentiometer with a Raspberry Pi.

Now, let's dive into that.

What is a Potentiometer?

A potentiometer is a variable resistor used to vary the resistance in a excursion by rotating a knob. This is used to vary voltages in any circuit or to command current flow in a circuit. Information technology contains iii pins; the offset i is used equally an input and the second is for ground connection, and the tertiary is situated in the corner as the output pivot.

Reading Analog Bespeak Data with a Potentiometer

At present let'due south build an case projection that uses the Raspberry Pi to detect the analog signal generated past a potentiometer. We are using a potentiometer hither as a simple example. The same principals can be applied to any other sensor that outputs an analog signal!

These are the components needed to build this project:

  • Raspberry Pi
  • 10K potentiometer
  • (ii) 1K Ohm resistors
  • 220 nF capacitor
  • Jumper wires
  • Breadboard

In one case y'all have all of the parts, connect everything to the Raspberry Pi following this wiring diagram:

Schematic

Before nosotros proceed, let me explain something more clearly.

JLPCB Ad

This entire circuitry depends on the charging and discharging of a capacitor, which, in plough, depends on how this excursion responds to an electrical pulse within a particular period of time. And so, we particularly target the fourth dimension to find the analog signal value. This time is dependent on the variable resistance of the resistor, which nosotros will change. All of these will be shown in the Raspberry Pi final.

In simple terms, the capacitor acts like a container of charge. As it fills with accuse, the voltage beyond it rises, only you cannot read the voltage every bit Raspberry Pi is incapable of reading analog voltage value. Thus, to read this value, we basically annotation the time information technology takes for the voltage to go above one.v-ane.6V, typically a value that registers a digital loftier. The charging speed depends upon the value of the variable resistor; the less the value, the faster the capacitor will charge.

The 2nd most important step to note here is that the capacitor needs to discharge completely before being used once more to become a good reading. So for this, nosotros use the resistor R1 connected to pivot 18 with the 10K variable resistor for completely charging the capacitor. On the other paw, nosotros use R2 for discharging the capacitor connected to pivot 24 in Raspberry Pi.

We will now apply the footstep-response indicate for four to 5 ms (well, generally 5ms). In that detail time, the capacitor will charge via R1 and variable resistor and will discharge via R2. The discharge fourth dimension hither is set to less than ane ms and is done via the python code shown below. As this cycle continues for charging and discharging, the lawmaking acts as a counter and provides values equivalent to the pace-response read by the Raspberry Pi. This is how the Raspberry Pi reads the entire analog bespeak with a potentiometer's help in a footstep-response excursion.

In summary, we basically use R1 and the potentiometer to charge the circuit together, and R2 to belch the excursion which is then read by the python code to give us the values for the step-response change with fourth dimension.

Python Code for Reading a Potentiometer

Subsequently building the circuit, copy the code below to a text editor in the Raspberry Pi terminal and save it equally a file named "pace-res.py".

            import RPi.GPIO equally GPIO import time  GPIO.setmode(GPIO.BCM)  pin_a = eighteen pin_b = 24  def discharge():     GPIO.setup(pin_a, GPIO.IN)     GPIO.setup(pin_b, GPIO.OUT)     GPIO.output(pin_b, False)     fourth dimension.sleep(0.004)  def charge_time():     GPIO.setup(pin_b, GPIO.IN)     GPIO.setup(pin_a, GPIO.OUT)     count = 0     GPIO.output(pin_a, True)     while not GPIO.input(b_pin):         count = count + i     return count  def analog_read():     belch()     return charge_time()  while True:     impress(analog_read())     time.slumber(1)                      

Run the code by typing the following command in the final:

            sudo python step-res.py          

Terminal Response

Later running the code, you volition start seeing the response in the terminal like the ane shown higher up. As you rotate the potentiometer, the value volition increase in one management and decrease in the other.

This is the all-time possible method to read analog signals of any sensor. Just note that nosotros did the pace-response technique instead of using physical analog to digital converters. If y'all have the resource, information technology's better to apply a hardware ADC with the Raspberry Pi as they are easier to employ and more accurate than the method to a higher place.

Thank you for reading and leave a comment beneath if you have questions about annihilation!

Raspberry Pi Read Ct Sensor Output Voltage on Gpio

Source: https://www.circuitbasics.com/using-potentiometers-with-raspberry-pi/