Rich's Wordpress

又一个WordPress站点

Analogue to Digital Converter (ADC)

The function of the ADC is to convert analogue signals into digital signals. Since computers can ultimately only recognise binary numbers, external analogue signals are often converted by the ADC into digital information that it can recognise. In Raspberry PI, a common application is to convert a changing voltage into a digital signal.

Raspberry Pi itself doesn’t contain an ADC, so by using an external ADC chip, the MCP3004, the Pi can measure voltage. The MCP3004 is a 10-bit 4-channel ADC chip, which means it can measure up to 4 input voltages simultaneously and has the ability to detect 1,024 (2^10) discrete analogue levels.

MCP3004 Chip (Voltage conversion chip) Pin function:

  1. CH0-CH3: Analogue inputs
  2. NC: No Connection (doesn’t have any usage)
  3. DGND: a pin connected to the digital ground plane
  4. VDD: 3.3 Volt power supply
  5. VREF: reference for voltage input, by which the ADC computes digital values.
  6. AGND: a pin connected to the analogue ground plane
  7. CLK: Serial clock
  8. DOUT: Serial data out
  9. DIN: Serial data in
  10. CS/SHDN: Chip Select/Shutdown Input

The pins 8, 9, 10, and 11 are for SPI protocol, a serial peripheral interface (SPI) is one of the most widely used interfaces between microcontroller and peripheral ICs such as sensors, ADCs, DACs.

This is the variable resister connected to the ADC.

Common Function for ADC

gpiozero.MCP3004(channel=0) #Initializing the ADC chip. The default channel for collecting data is 0

MCP3004.value #It returns the current value read from the device, scaled to a value between 0 and 1

Flow Chart

Import the related module and initialize the module. The ADC value is read continuously in the loop, it is then converted into a voltage value, and printed out every 1 second.

Code:

from gpiozero import MCP3004
from time import sleep

adc = MCP3004(channel  = 0)

while True:
	V = adc.value*3.3
	print("%.2f"%V+"V")
	sleep(1)

Final Result:

A variable resistor is connected to the ADC, by spinning it, it changes the resistance of the variable resistor, thus changing the voltage across it.

The voltage detected by the ADC will be printed on my computer IDE.

Analogue to Digital Converter (ADC)
Scroll to top