Jump to content


Photo

Wiring of DHT22 to pcduino v1


  • Please log in to reply
36 replies to this topic

#1 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:54 AM

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?   



#2 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:54 AM

I would 3.3v and a 4.7k to 10k pullup.



#3 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:55 AM

Thanks for your quick answer, but why 3,3v? 
DHT22 datasheet says "Power supply: 3.3-6V DC".
So, 5v close to middle of it range?



#4 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:55 AM

Because pcDuino is 3.3v input.



#5 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:55 AM

 Thank you very much, I'll try such schematics  smiley.gif



#6 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:55 AM

Could anybody appoint me on sample code to read this DHT22 values (pcduino v1, ubuntu)? Python is preferable.



#7 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:55 AM

I have working python code to read dht22 on pcduino3. The pcDuino 1 may not be fast enough.



#8 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:55 AM

I met a lot of code for RPi, tried it but it not works on my board. So, RPi board seems to have better performance than v1?



#9 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:55 AM

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.



#10 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:55 AM

Ok, I'll wait this post respectfully. What can you say about C code for this routine?



#11 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:55 AM

Compiled C worked for me on my pcduino1.



#12 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:56 AM

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):



#13 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:56 AM

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 mytries<0:
x=1
while x>0:
x=self.getdht22(mypin)
elif mytries>0:



#14 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:56 AM

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
 
 


#15 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 15 February 2015 - 03:56 AM

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



#16 Frank

Frank

    Advanced Member

  • Members
  • PipPipPip
  • 35 posts
  • LocationSaint-Nazaire - France

Posted 26 March 2015 - 02:20 PM

Good job but i think something is missing. Where is getDHT22 define ???


Frank


#17 cwilt

cwilt

    Advanced Member

  • Members
  • PipPipPip
  • 1,012 posts

Posted 26 March 2015 - 02:30 PM

This was a long time ago. I will have to find my code and post it.

#18 Frank

Frank

    Advanced Member

  • Members
  • PipPipPip
  • 35 posts
  • LocationSaint-Nazaire - France

Posted 26 March 2015 - 02:55 PM

Thank's


Frank


#19 cwilt

cwilt

    Advanced Member

  • Members
  • PipPipPip
  • 1,012 posts

Posted 26 March 2015 - 09:53 PM

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()


  • Frank likes this

#20 cwilt

cwilt

    Advanced Member

  • Members
  • PipPipPip
  • 1,012 posts

Posted 26 March 2015 - 09:56 PM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users