srouth
Posts: 2
Joined: Wed Jul 15, 2015 1:14 pm

GPIO

Wed Jul 15, 2015 1:21 pm

I am new to RPi, I have got a RPi2 model B. I will be taking multiple analog inputs and I wish to process these signals individually, whithout being interdependent on one another. What is the best way of doing that via python??

Kindly help!!

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: GPIO

Wed Jul 15, 2015 2:06 pm

Unlike the Arduino the RPi does not have any analog input pins. You will have to use an external device. The MCP3008 is one possibility.

srouth
Posts: 2
Joined: Wed Jul 15, 2015 1:14 pm

Re: GPIO

Wed Jul 15, 2015 2:24 pm

YEs I will use ADC fr that. My main question here is there a way to code each channel of I/P separately, independtly on one another. Consider I have 4 inputs and I want to code them independtly at the same time. How to do that??

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: GPIO

Wed Jul 15, 2015 2:35 pm

srouth wrote:My main question here is there a way to code each channel of I/P separately, independtly on one another. Consider I have 4 inputs and I want to code them independtly at the same time. How to do that??
What do you mean with "code" an input?
what do you mean with "at the same time"?
What precision are you looking for? in ADC precision and in "same time precision"

User avatar
mikronauts
Posts: 2743
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: GPIO

Wed Jul 15, 2015 2:39 pm

Read the following article, carefully:

http://www.mikronauts.com/raspberry-pi/ ... and-howto/

If you need only eight inputs, you can ignore the 74hc138 and device selection.
srouth wrote:I am new to RPi, I have got a RPi2 model B. I will be taking multiple analog inputs and I wish to process these signals individually, whithout being interdependent on one another. What is the best way of doing that via python??

Kindly help!!
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: GPIO

Wed Jul 15, 2015 3:49 pm

If you are going to drive a MCP3008 directly from the GPIO pins you will probably have to use a python library. Spidev is one possibility, pigpio is another.

This code uses pigpio.

Code: Select all

# pio.py
# classes using Pigpio
# written by Roger Woollett
import pigpio as pg
import socket as soc

class Pio():
	# to override the default host name (ip address) and port
	# assign to Pio.host and/or Pio.port in the calling file
	
	# static data
	_started = False
	_refcount = 0
	
	def __init__(self):
		Pio._refcount += 1
		self._closed = False
		if not Pio._started:
			
			try:
				Pio.host
			except AttributeError:
				Pio.host = 'localhost'
				
			try:
				Pio.port
			except AttributeError:
				Pio.port = 8888
				
			# now see if daemon is running
			try:
				socket = soc.create_connection((Pio.host,Pio.port))
			except:
				dok = False
			else:
				socket.close()
				dok = True
			
			if not dok:
				self._closed = True
				raise RuntimeError('Cannot connect to pigpio daemon')
					
			Pio.pi = pg.pi(Pio.host,Pio.port)
			Pio._started = True
			
	def close(self):
		Pio._refcount -= 1
		self._closed = True

		if Pio._refcount == 0:
			Pio.pi.stop()
			Pio._started = False
			
	def __del__(self):
		if not self._closed:
			raise RuntimeError('Close not called in Pio object')

class ADC(Pio):
	# class to access a MCP3008 A to D converter
	# pins used are:
	# Name board(gpio) - chip pin
	# MOSI	19(GPIO10)	11 - data in
	# MISO	21(GPIO9)	12 - data out
	# SCLK	23(GPIO11)	13 - clock
	# CE0	24(GPIO8)	10 - chip select
	# CE1	26(GPIO7)	10 - chip select on second chip
	# There is a second set of pins used on B+ and Pi2
	# This auxilliary port is not supported as yet
	def __init__(self,chip):
		Pio.__init__(self)
		# chip should be 0 (chip connected to CE0) or 1 (CE1)
		if chip < 0 or chip > 1:
			Pio.close(self)
			raise RuntimeError('chip must be 0 (CE0) or 1 (CE1)')
			
		self.handle = Pio.pi.spi_open(chip,50000,0)
		
	def close(self):
		Pio.pi.spi_close(self.handle)
		Pio.close(self)
		
	def read(self,channel):
		# channel corresponds to pins 1 (ch0) to 8 (ch7)
		# we have to send three bytes
		# byte 0 has 7 zeros and a start bit
		# byte 1 has the top bit set to indicate
		# single rather than differential operation
		# the next three bits contain the channel
		# the bottom four bits are zero
		# byte 2 contains zeros (don't care)
		# 3 bytes are returned
		# byte 0 is ignored
		# byte 1 contains the high 2 bits
		# byte 2 contains the low 8 bits
		if channel < 0 or channel > 7:
			raise RuntimeError('channel must be 0 - 7')
			
		(count,data) = Pio.pi.spi_xfer(self.handle,[1,(8 + channel) << 4,0])
		if count > 0:
			return (data[1] << 8) + data[2]
		else:
			return 0
To use it you put it into a file called pio.py in the same directory as your code and then put this line in your code

Code: Select all

from pio import Pio,ADC
You will need to install pigpio (http://abyz.co.uk/rpi/pigpio/) and make sure its daemon is running bfore you start your program.

You create an instance of ADC to represent the MCP3008 and then call its read function to read a specific channel. loading the pigpio object (pi) is done for you by the Pio base class. See the comments in the code for more details.

Return to “Beginners”