Jump to content
jim

Wiring of DHT22 to pcduino v1

Recommended Posts

Hi guys!
Tell me please how can I properly connect the DHT22 sensor to my pcduino v1 board? There are several different ways described in the net, but i didn't find about wiring it to pcduino. What pin should I use: 5v or 3.3v ? And what could you say about pull-up resistor?   

Share this post


Link to post
Share on other sites

I don't have an rpi but it is my understanding that the difference is in the drivers.  Allwinner has given out the information regarding register address for direct access to IO. Once I clean up the python code I will post it.

Share this post


Link to post
Share on other sites

I am going to make a few posts with different python modules. This post contains the routines I use for fast read and write of gpio files. I just started writing python a week ago so it is not pretty and there is no error catching. My plan is to add PWM, SPI, I2C, code for pcduino to this one module.


I will post this on linksprite forum too.


MypcDuinoIO.py

Code: [select]

import os


class pin:
   GPIO_MODE_PATH= os.path.normpath('/sys/devices/virtual/misc/gpio/mode/')
   GPIO_PIN_PATH=os.path.normpath('/sys/devices/virtual/misc/gpio/pin/')
   GPIO_FILENAME="gpio"


   def __init__(self,mypin,mode):
      self.gpio=mypin
      self.mode=mode
      self.pinMode=(os.path.join(self.GPIO_MODE_PATH, 'gpio'+str(self.gpio)))
      self.pinData=(os.path.join(self.GPIO_PIN_PATH, 'gpio'+str(self.gpio)))
      file=open(self.pinMode,'r+')
      file.write(str(self.mode))
      file.close
      self.file=open(self.pinData,'r+',0)
      
   def high(self):
      self.file.write("1")
      self.thepin=1
      
   def low(self):
      self.file.write("0")
      self.thepin=0
   
   def toggle(self):
      if self.thepin==0:
         self.high()
      else:
         self.low()
   
   def state(self):
      self.file.seek(0)
      return int(self.file.read(1))
      
   def setmode(self,x):
      self.mode=x
      file2=open(self.pinMode,'r+')
      file2.write(str(self.mode))
      file2.close
         
   def __del__(self):

Share this post


Link to post
Share on other sites

In this post will be the code to read dht22. It is still very much in development stages so its very ugly code but does work for me.


dht22.py

Code: [select]

import MypcDuinoIO
import time
from fractions import Fraction


class dht22:

def read(self,pin,retries):
mypin=pin
mytries=retries
if mytries==0:
x=self.getdht22(mypin)
elif mytriesx=1
while x>0:
x=self.getdht22(mypin)
elif mytries>0:

Share this post


Link to post
Share on other sites
Last is my test program.


Code: [select]
import dht22


MyDHT22=dht22.dht22()


## read(pin number, retries)
## 0 retries means it tries only once
## greater than 0 and it will retry that number of times
## less than 0 ad it will retry forever until it gets a good CRC check
test=MyDHT22.read(2,0)
## The return = 0 if its good anything else is an error
print test
## Temperature in degress celcius
print MyDHT22.temperature()
## Humidity in %
print MyDHT22.humidity()
## Calculated dew point in degress celcius
print MyDHT22.dewpoint()
modify_inline.gif
 
 

Share this post


Link to post
Share on other sites

Great! It works on my v1 board! Thank you very much! 
But most of times it returns 2 or 3 instead of zero. When 2 returned, the values looks accurate. 
Sorry, I didn't find this topic on linksprite forum

Share this post


Link to post
Share on other sites

dht22.py

import MypcDuinoIO
import time
from fractions import Fraction

class dht22:
	
	def read(self,pin,retries):
		mypin=pin
		mytries=retries
		if mytries==0:
			x=self.getdht22(mypin)
		elif mytries<0:
			x=1
			while x>0:
				x=self.getdht22(mypin)
		elif mytries>0:
			for i in range(0,mytries):
				x=self.getdht22(mypin)
				if x==0:
					break
				else:
					time.sleep(0.1)
		return x
					
	def getdht22(self,pin):
		myerror=0
		testpin=pin
		Mybits=''
		Mylist=[]
		count=1
		MyPin = MypcDuinoIO.pin(testpin,1)
		MyPin.low()
		time.sleep(0.0006)
		MyPin.setmode(0)
		for i in range(0,41):
			while MyPin.state()==0:
				pass
			while (MyPin.state()==1 and count<20):
				count += 1
			if count>=20:
					myerror=1
					break
			if count>2:
				Mylist.append('1')
			else:
				Mylist.append('0')
			count=1
		Mybits=''.join(Mylist)
		if len(Mybits)<41:
			self.tempc=0
			self.humid=0
			return 3
		temp=int(Mybits[1:17],2)+int(Mybits[18:33],2)
		if temp>255:
			temp=temp-256
		crc=int(Mybits[33:41],2)
		if temp==crc:
			myerror=0
		else:
			myerror=2
		MyTemp=int(Mybits[18:33],2)
		if MyTemp>32767:
			MyTemp=32768-MyTemp
		self.tempc=MyTemp/10.0
		self.humid=int(Mybits[1:17],2)/10.0
		return myerror
		
	def temperature(self):
		return self.tempc
		
	def humidity(self):
		return self.humid

	def dewpoint(self):
		expo=Fraction('1/8')
		temp=(self.humid/100)**expo
		temp2=temp*(112+(0.9*self.tempc))+(0.1*self.tempc)-112
		return "%.2f"%temp2

MypcDuinoIO.py

import os

class pin:
	GPIO_MODE_PATH= os.path.normpath('/sys/devices/virtual/misc/gpio/mode/')
	GPIO_PIN_PATH=os.path.normpath('/sys/devices/virtual/misc/gpio/pin/')
	GPIO_FILENAME="gpio"
	thepin=''

	def __init__(self,mypin,mode):
		self.gpio=mypin
		self.mode=mode
		self.pinMode=(os.path.join(self.GPIO_MODE_PATH, 'gpio'+str(self.gpio)))
		self.pinData=(os.path.join(self.GPIO_PIN_PATH, 'gpio'+str(self.gpio)))
		file=open(self.pinMode,'r+')
		file.write(str(self.mode))
		file.close
		self.file=open(self.pinData,'r+',0)
		
	def high(self):
		self.file.write("1")
		self.thepin=1
		
	def low(self):
		self.file.write("0")
		self.thepin=0
	
	def toggle(self):
		if self.thepin==0:
			self.high()
		else:
			self.low()
	
	def state(self):
		self.file.seek(0)
		return int(self.file.read(1))
		
	def setmode(self,x):
		self.mode=x
		file2=open(self.pinMode,'r+')
		file2.write(str(self.mode))
		file2.close
			
	def __del__(self):
		self.file.close
		
class adc:
	ADC_PATH=os.path.normpath('/proc/')
	raw=0
	
	def __init__(self,pin):
		self.adc=pin
		if self.adc==0 or self.adc==1:
			self.adcMult=2/62
		else:
			self.adcMult=3.3/4095
		self.adcData=(os.path.join(self.ADC_PATH, 'adc'+str(self.adc)))
		self.file=open(self.adcData,'r')
		
	def raw(self):
		self.file.seek(0)
		value=self.file.read(9)
		value=int(value[5:len(value)])
		return value
		
	def volts(self):
		self.file.seek(0)
		value=self.file.read(9)
		value=int(value[5:len(value)])
		if value==0:
			return 0
		else:
			return '%.4f'%(value*self.adcMult)
		
	def __del__(self):
		self.file.close	

test22.py

import dht22

MyDHT22=dht22.dht22()

## read(pin number, retries)
## 0 retries means it tries only once
## greater than 0 and it will retry that number of times
## less than 0 ad it will retry forever until it gets a good CRC check
test=MyDHT22.read(2,0)
## The return = 0 if its good anything else is an error
print test
## Temperature in degress celcius
print MyDHT22.temperature()
## Humidity in %
print MyDHT22.humidity()
## Calculated dew point in degress celcius
print MyDHT22.dewpoint()

Share this post


Link to post
Share on other sites

Please note that this will not always return a value due to the speed of pcDuino IO functions. I tested these methods many times on my pcDuino3 until I had it as fast as possible.

 

Hope it works for you.

Share this post


Link to post
Share on other sites
Hello,

no way to have a measure corresponding to the checksum. I think it's because "111" or "1111" is always "1", but sometimes "11" is "1" and the other "0".

Yet the values of humidity and temperature  returned are relatively correct (without checksum).

In c it work(http://learn.linksprite.com/pcduino/arduino-ish-program/humidity-and-temperature-sensor-dht11-on-pcduino/). Is it possible to create a library in c compiled and called using python? I think so but I do not know how.

Frank

Share this post


Link to post
Share on other sites

I was worried that the pcDuino1 would not be fast enough. When I wrote that I was working with pcDuino3.

 

You could use the arduino ide that is on the pcDuino1 to write the c code.

 

I searched the forums and came up with this discussion. With a few modifications it could be used as a command line utility and then called from python.

 

Odd. In my testing on the pcDuino3 my python code was just as fast at reading the bits as compiled c was.

Share this post


Link to post
Share on other sites

No nothing.

 

I took a logic analyzer in my work to see if it's not from the component. I come back to this post after measurements.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×