You are on page 1of 18

6/27/2016

MoreSensorExamplesMeasuringStuff

Measuring
Stuff
Home
MeettheArduinos
SensorExamples
MoreSensorExamples
TheArduinoDAQChronicles
MeanderingArduinoNotes
DataStorage&Retrieval
Display&Calculate
Links&References

Searchthissite

MoreSensorExamples
Contents
1HallEffectRPMCounter
1.1Parts:
1.2Wiring:
1.3DisplayExample:
1.4Comments:
1.5Explanation&Code:
2HSM20GHumidityandTemperatureMiniboard
2.1Parts:
2.2Wiring:
2.3DisplayExample(serialmonitor):
2.4Comments:
2.5Code:
3500gramLoadCell
3.1Parts:
3.2Wiring:
3.3DisplayExample:
3.4Comments:
3.5Code:
4I2C7SEGMENTDisplay(okay,itsnotasensor)
4.1Parts:
4.2Wiring:
4.3Comments:
4.4Code:
5WorkInProgress

HallEffectRPMCounter
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

1/18

6/27/2016

MoreSensorExamplesMeasuringStuff

Whatitis
called:
Whatit
does:
Whatit
takes:
Whatpins
areused:
What
libraries:

RPMCounterUsingAHallEffectCounter
CalculatesRPM
Arduino(5V),HallEffectSensor,10K
Resistor,Magnets
HallEffectSensorusesanalog0,16x2LCD
usesdigital7,8,9,10,11,12
LiquidCrystal

Parts:
Boarduino(any5Varduino)
LCDusedinparallelmode
HallEffectSensor
10Kresistor,10KpotforLCDcontrast,optionalcapacitors
(fornoisyenvironments)
Magnets
HousingforMagnets(youmakeitforyourapplication).
ThehousingIused.Thepartslipsontotheshaft,anduses
four3mmmagnetsstacked2and2.Thesensorneedstobe
within1/4inchofthemagnets.Themagnetsmustbe
stackedtoprovideanorthandsouthpole.Thisparticular
HallEffectsensorflipsstatesonNandS(twotimesper
revolution).NotallHallEffectsensorsworkthatway!Read
thedatasheetforyoursensor.

Wiring:

https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

2/18

6/27/2016

MoreSensorExamplesMeasuringStuff

DisplayExample:

Comments:
Adafruithassomegreattutorialstohelpwiththisproject.
First,hookinguptheLCDisshownhereinfantasticdetail.
ThedescriptionoftheHallEffectsensorisnotasdetailed:
Runsat3.5Vupto24V.Touseconnectpowertopin1
(allthewaytotheleft),groundtopin2(middle)and
thena10Kpullupresistorfrompin3topower.Then
listenonpin3,whenthesouthpoleofamagnetisnear
thefrontofthesensor,pin3willgodownto0V.
Otherwiseitwillstayatwhateverthepullupresistor
isconnectedto.Nothingoccursifamagnet'snorthpole
isnearby(unipolar).

But,itgetsthebasicsacross.
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

3/18

6/27/2016

MoreSensorExamplesMeasuringStuff

Explanation&Code:
Thisparticularprojectsamplesat1000msintervals,andit
averagessamplesoverthatperiod.ChangesampleTimefor
fasterrates.ThesimplebargraphusesmaxRPMfora100%
spread,sochangeasneeded.
/*
RPMmetercode(testedondevicerotatingat
10,000rpm)
rev.001
*usingahalleffectsensor
http://www.adafruit.com/index.php?
main_page=product_info&cPath=35&products_id=158
*usingaparallelLCDwith
LiquidCrystalLibrary
http://www.ladyada.net/learn/lcd/charlcd.html
http://www.arduino.cc/en/Tutorial/LiquidCrystal
withpins7,8,9,10,11,12
*/
//includethelibrarycode:
#include<LiquidCrystal.h>
//initializethelibrarywiththenumbersof
theinterfacepins
LiquidCrystallcd(7,8,9,10,11,12);
//readthehalleffectsensoronpin2
constinthallPin=2;
constunsignedlongsampleTime=1000;
constintmaxRPM=10200;
voidsetup()
{
pinMode(hallPin,INPUT);
Serial.begin(9600);
//setuptheLCD'snumberofrowsand
columns:
lcd.begin(16,2);
//PrintamessagetotheLCD.
lcd.print("initializing");
delay(1000);
lcd.clear();
}
voidloop()
{
delay(100);
intrpm=getRPM();
lcd.clear();
displayRPM(rpm);
displayBar(rpm);
}

intgetRPM()
{
//sampleforsampleTimeinmillisecs
intkount=0;
booleankflag=LOW;
unsignedlongcurrentTime=0;
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

4/18

6/27/2016

MoreSensorExamplesMeasuringStuff

unsignedlongstartTime=millis();
while(currentTime<=sampleTime)
{
if(digitalRead(hallPin)==HIGH)
{
kflag=HIGH;
}
if(digitalRead(hallPin)==LOW&&
kflag==HIGH)
{
kount++;
kflag=LOW;
}
currentTime=millis()startTime;
}
intkount2rpm=
int(60000./float(sampleTime))*kount;
returnkount2rpm;
}

voiddisplayRPM(intrpm)
{
lcd.clear();
//setthecursortocolumn0,line1
lcd.setCursor(0,0);
//printthenumberofsecondssincereset:
lcd.print(rpm,DEC);
lcd.setCursor(7,0);
lcd.print("RPM");
}
voiddisplayBar(intrpm)
{
intnumOfBars=map(rpm,0,maxRPM,0,15);
lcd.setCursor(0,1);
if(rpm!=0)
{
for(inti=0;i<=numOfBars;i++)
{
lcd.setCursor(i,1);
lcd.write(1023);
}
}
}

HSM20GHumidityandTemperature
Miniboard
Whatitis
called:
Whatitdoes:

HSM20GHumidityandTemperature
Sensor
Measures%RelativeHumidityand
Temperature
Whatittakes: Arduino,HSM20GSensor
Whatpinsare +5,Gnd,plustwoanalogpins(0,1inthis
used:
example)
Whatlibraries None
areneeded:

https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

5/18

6/27/2016

MoreSensorExamplesMeasuringStuff

Parts:
Arduino(any5volt,orpossiblya3.3voltwithexternal5volt
supply)
HSM20G
(1)10K,(1)100Kresistors
(1)47uFcapacitor

Wiring:

https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

6/18

6/27/2016

MoreSensorExamplesMeasuringStuff

DisplayExample(serialmonitor):
34%RH
25.8degC
78.4degF

Comments:
Thespecsheetprovidesthefollowingcurvedata:
Humidity
Volts,RelativeHumidityin%
0.74,10
0.95,20
1.31,30
1.68,40
2.02,50
2.37,60
2.69,70
2.99,80
3.19,90
Thecurvefitforthisdatais:
RH=31*volts12
So,assuminga5voltreference,theArduincodewouldbe:
RH=(31*analogRead(aPin)*5/1023)12or,RH=
0.1515*analogRead(aPin)12.0
Temperature
ResistanceinKohms,TemperatureinC
163,81,0
97.10,10
59.42,20
47.0,25
37.43,30
24.19,40
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

7/18

6/27/2016

MoreSensorExamplesMeasuringStuff

16.01,50
10.83,60
Thecurvefitforthisdatais:
TemperatureinC=281.583*1.0230^(1.0/R)*R^0.1227
150.6614,orinArduinospeak,T=281.583*pow(1.0230,
(1.0/R))*pow(R,0.1227)150.6614
SowehaveTemperaturegivenanRTDvalue.Butwhatis
theRTDvalue?Thisisalittletrickier.Wehaveavoltage
readingatthejunctionoftheRTDandthe10Kresistor.We
havetoassumethatthevoltageacrosstheRTDand10K
seriesresistoris5volts(pulltheresistorandyou'llgeta
readingof1022onthepin).So,let'sdoanexample:
At20degreesC,theRTDresistanceis59.42Kohms.
Current(i),isanalogvoltagedividedby10Kohms.Thesame
currentflowsthroughtheRTD,soweknowRTDinKohms
=(5voltsanalogvoltage)/i,so
RTDinKohms=(5analogvoltage)*10K/analogvoltage.So,
wegettheanalogvoltage,calculateRTDanduseacurvefit
togiveustemperatureinC.TheArduinocodeis:
intVraw=analogRead(aPin)
floatV=(float)Vraw*5.0/1023.0
floatR=(5.0V)*10.0/V//dropthe1000(K)
floatT=281.583*pow(1.0230,(1.0/R))*pow(R,0.1227)
150.6614

Code:
/*
HGM20GRev.1
http://sites.google.com/site/measuringstuff
*/
constbytensum=10;
inthumidityPin=1;
intThgm20Pin=2;
unsignedintsensorValue2=0;//variableto
storethevaluecomingfromthesensor
unsignedintsensorValue3=0;//variableto
storethevaluecomingfromthesensor
voidsetup(){
Serial.begin(9600);
}
voidloop(){
for(bytei=0;i<nsum;i++)
{
sensorValue2+=analogRead(humidityPin);
sensorValue3+=analogRead(Thgm20Pin);
}
intsensorValue2Avg=sensorValue2/nsum;
floatRH=0.1515*sensorValue2Avg12.0;

intsensorValue3Avg=sensorValue3/nsum;
floatVt=(float)sensorValue3Avg*5.0/1023.0;
floatR=(5.0Vt)*10.0/Vt;
floatTinC=281.583*pow(1.0230,
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

8/18

6/27/2016

MoreSensorExamplesMeasuringStuff

(1.0/R))*pow(R,0.1227)150.6614;
floatTinF=TinC*(9.0/5.0)+32;

Serial.print(RH,0);
Serial.println("%RH");
Serial.print(TinC,1);
Serial.println("degC");
Serial.print(TinF,1);
Serial.println("degF");

sensorValue2=0;
sensorValue3=0;
delay(5000);
}

500gramLoadCell
Whatitis
called:
Whatitdoes:
Whatittakes:
Whatpinsare
used:
Whatlibraries
areneeded:

LoadCell,500gram
Weightmeasurement
loadcell,opamp,arduino
digital3,digital4,analog0
optionalserialLCDlibrary

Parts:
loadcell
LM358opamp
100Kresistors(2)
optionalserialLCD

Wiring:

https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

9/18

6/27/2016

MoreSensorExamplesMeasuringStuff

DisplayExample:
200grams

Comments:
Resistorvaluesmayneedtobereduced(85K/85Kinsteadof
100K/100K)togettheamplifiedvoltagerangeinsidethe0to
1.1voltanalogreference.Youneedthereducedreferenceto
giveyouthebestresolution.Zeroingisdonewith
aninterruptattachedtopin3(interrupt1).

Code:
/*
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

10/18

6/27/2016

MoreSensorExamplesMeasuringStuff

loadcellRev.1

http://sites.google.com/site/measuringstuff

*/

#include"SparkSoftLCD.h"

//curvefitforanalogvoltsasint(01023)to
grams
constfloatslope=0.8319;
constfloatconstant=113.27;
//numberofsamplestotakeforaverage
constbyteavgNum=100;
//serialreclinehere
constbytelcdTxPin=4;
//analogreadpin
constbyteaPin=0;
//checkforzeroingroutinerequest
//int1istiedtopin3
constbytezeroChkPin=3;
constbyteaRefVolts=1.1;
SparkSoftLCDLcd=SparkSoftLCD(lcdTxPin);
floatoffset=0;
longvRawWeight=0;
intgrams=0;
floatg=0;
bytezeroPinStatus=0;
bytezeroRoutineFlagSet=0;
voidsetup()
{
Serial.begin(9600);
pinMode(lcdTxPin,OUTPUT);
pinMode(zeroChkPin,INPUT);
attachInterrupt(1,zeroRoutineSet,LOW);
Lcd.begin(9600);
//switchto1.1internalreferencevoltageand
stabilize
analogReference(INTERNAL);
vRawWeight=analogRead(aPin);
delay(10);
vRawWeight=analogRead(aPin);
//zerothedisplay
zeroRoutine();
delay(500);
}
voidloop()
{
calcGrams();
//uncommentthislineforcalibration
//Serial.println(vRawWeight,DEC);
Lcd.clear();
Lcd.print(grams,DEC);
Lcd.cursorTo(1,5);
Lcd.print("grams");
//checkforzerorequest
if(zeroRoutineFlagSet==1)
{
zeroRoutine();
zeroRoutineFlagSet=0;
}
}
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

11/18

6/27/2016

MoreSensorExamplesMeasuringStuff

voidcalcGrams()
{
vRawWeight=0;
//takeavgNumsamples
for(inti=0;i<avgNum;i++)
{
intavalue=analogRead(aPin);
vRawWeight=vRawWeight+avalue;
delay(5);
}
vRawWeight=vRawWeight/avgNum;
//calculategramsandroundupwardif
necessary
g=slope*(float)vRawWeight+constant+offset;
grams=(int)g;
floatremainder=g(float)grams;
if(remainder>0.5)grams=grams+1;
}
voidzeroRoutine()
{
offset=0;
Lcd.clear();
Lcd.print("zeroing...");
//checkzeroandoffsetifneeded
calcGrams();
if(grams!=0)offset=g;
}
voidzeroRoutineSet()
{
zeroRoutineFlagSet=1;
}

I2C7SEGMENTDisplay(okay,itsnot
asensor)
Whatitis
called:
Whatitdoes:
Whatittakes:
Whatpinsare
used:
Whatlibraries
areneeded:

i2c7segmentdisplay
providesa4digitnumericaldisplay
displayandArduinowithi2cbreakout
analog4(sda),analog5(sdl)on
Duemilanove
thecodehasnotbeenplacedinalibrary
(yet)

Parts:
i2c7seg

Wiring:
Wiringiseasy.Plus5vtopin1(left),groundtopin2,analog
4topin3andanalog5topin4.

https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

12/18

6/27/2016

MoreSensorExamplesMeasuringStuff

Comments:
It'snotasensorbutitrunsonthei2cbuslikemanysensors
do,andsohereitis.
Thisisaverybrightdisplaythatcanbeseenfromsome
distance,andthatisreallyabouttheonlyreasontouseit
insteadofaserialLCDdisplay,especiallysinceitcostabout
thesame.Youarealsolimitedtomostlynumberswithafew
cobbledtogetherletters.Can'treallyseemuchuseforit
exceptwhereyouaremeasuringvoltages,orsomenumeric
valueandwantalarge,wellilluminatedoutput.Ifthisthing
soldfor$10I'dbedrooling$23isjusttoomuch.

Code:
ThefirstcodesetisdirectlyfromtheGravitechwebsite.
/**********************************************************
ExampleprogramI2C7SEGinterfacewith
Arduino.

SETUP:I2C7SEG=>Arduino
PIN1=>+5VPIN2=>GND,PIN3=>A4,
PIN4=>A5
Note:Theprogramiswrittenforaddress
0x70(Arduinoaddress0x38).
ThisprogramwastestedusingArduino
Nano
Document:SAA1064datasheet
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

13/18

6/27/2016

MoreSensorExamplesMeasuringStuff

Updated:July30,2008
Email:support@gravitech.us
Gravitech
(C)Copyright2008AllRightsReserved
**********************************************************/

#include<Wire.h>

voidsetup()
{
Wire.begin();//joini2cbus(address
optionalformaster)
delay(500);
}

voidloop()
{
constintlookup[10]=
{0x3F,0x06,0x5B,0x4F,0x66,

0x6D,0x7D,0x07,0x7F,0x6F};
intCount,Thousands,Hundreds,Tens,Base;

Wire.beginTransmission(0x38);
Wire.send(0);
Wire.send(B01000111);
Wire.endTransmission();

for(Count=0;Count<=9999;Count++)
{
Wire.beginTransmission(0x38);
Wire.send(1);
Thousands=Count/1000;
Hundreds=(Count(Thousands*1000))/100;
Tens=(Count((Thousands*1000)+
(Hundreds*100)))/10;
Base=Count((Thousands*1000)+
(Hundreds*100)+(Tens*10));
Wire.send(lookup[Base]);
Wire.send(lookup[Tens]);
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

14/18

6/27/2016

MoreSensorExamplesMeasuringStuff

Wire.send(lookup[Hundreds]);
Wire.send(lookup[Thousands]);
Wire.endTransmission();
delay(10);
}
}

Gravitechalsomakesa7segmentshield,andthe
codeforithassomeadditionalcluesabout
decimalpointsandsuch.So,Itooktheirmore
completeshieldexamplecode,addedmyown
temperaturesensor,thewonderfulTM36(hooked
toanalog0),andcreatedatemperaturedisplay.
Theinnerworkingsofthedisplayareasfollows:
0isallsegmentsoff,andthefollowingnumbers
controleachsegment:
1

32|64|2

16||4
o128(thedecimalpoint)
8
ThereforeifIwantathefirstLEDsegmentto
displaya1,Isend:
Wire.send(1)//somethingiscoming
Wire.send((2+4))//Iwantsegments2and4
turnedon
Wire.endTransmission()
Thenumbersare:0=63(1+2+4+8+16+32),1=6
(2+4),2=91(1+2+64+16+8),3=79,4=102,5=109,
6=125,7=7,8=127,9=111
IfIwanttodisplay3.145Iwouldsend:
Wire.send(1)//it'scoming
Wire.send(109)
Wire.send(102)
Wire.send(6)
Wire.send((79+128))
Wire.endTransmission()
Thereisnowaytoskipoveradigit(updatedigit3
withoutchnagingdigit2).Youmustsendthe
entirestring.
/**********************************************************
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard
15/18

6/27/2016

MoreSensorExamplesMeasuringStuff

/**********************************************************
ExampleprogramI2C7SEGinterfacewith
Arduino.
SETUP:I2C7SEG=>Arduino
PIN1=>+5VPIN2=>GND,PIN3=>A4,
PIN4=>A5
Note:Theprogramiswrittenforaddress
0x70(Arduinoaddress0x38).
ThisprogramwastestedusingArduino
Nano
Document:SAA1064datasheet
Updated:July30,2008
Email:support@gravitech.us
Gravitech
(C)Copyright2008AllRightsReserved
9/26/10
http://sites.google.com/site/measuringstuff
AddedcodeforanLM36temperaturesensorread
byanalog0
Displaystemperatureaftercountingto999
**********************************************************/
#include<Wire.h>
#defineBAUD(9600)/*Serialbauddefine*/
#define_7SEG(0x38)/*I2Caddressfor7
Segment*/
//placeotheri2caddressesheree.g.
//#defineEEP(0x50)/*I2Caddressfor
sometypeofEEPROM*/
constbytetPin=0;
//segmentsarefromtopclockwise
1,2,4,8,16,32,and64isthemiddlesegment
//codesfornumbers09,foradecimalpoint
add128
constbytelookup[10]=
{63,6,91,79,102,109,125,7,127,111};
intCount,Thousands,Hundreds,Tens,Ones;
voidsetup()
{
//Serial.begin(9600);
Wire.begin();//joini2cbus(address
optionalformaster)
delay(500);
initialize();
delay(500);
test7segment();
delay(500);
clearDisplay();
delay(500);
}
voidloop()
{
intrawV=analogRead(tPin);
floatv=(float)rawV*5.0/1023.0;
floattC=(100*v)50;
inttF=(9*tC/5)+32;
sendTempInF(tF);
//Serial.println(tF,DEC);
delay(5000);
}

voidinitialize()
{
//Configure7Segmentto12mAsegmentoutput
current,
//Dynamicmode,andDigits1,2,3AND4are
NOTblanked
Wire.beginTransmission(_7SEG);
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

16/18

6/27/2016

MoreSensorExamplesMeasuringStuff

Wire.beginTransmission(_7SEG);
Wire.send(0);
Wire.send(B01000111);
Wire.endTransmission();
clearDisplay();
}
voidclearDisplay()
{
Wire.beginTransmission(_7SEG);
Wire.send(1);
Wire.send(0);
Wire.send(0);
Wire.send(0);
Wire.send(0);
Wire.endTransmission();
}
voidtest7segment()
{
//Test7Segment
for(bytecounter=0;counter<8;counter++)
{
Wire.beginTransmission(_7SEG);
Wire.send(1);
for(bytecounter2=0;counter2<4;
counter2++)
{
Wire.send(1<<counter);
}
Wire.endTransmission();
delay(250);
}
}
voidsendTempInF(intnumber)
{
Wire.beginTransmission(_7SEG);
Wire.send(1);
Count=number;
Thousands=Count/1000;
Hundreds=(Count(Thousands*1000))/100;
Tens=(Count((Thousands*1000)+
(Hundreds*100)))/10;
Ones=Count((Thousands*1000)+
(Hundreds*100)+(Tens*10));
//113isthecodeforF(fahrenheit)
//cwouldbe64+16+8
Wire.send(113);
Wire.send(lookup[Ones]);
Wire.send(lookup[Tens]);
if(Hundreds==0)Wire.send(0);
elseWire.send(lookup[Hundreds]);
//Wire.send(lookup[Thousands]);
Wire.endTransmission();
delay(10);
}

Worksgreat!Displaycoststoomuch.

WorkInProgress

Comments
https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

17/18

6/27/2016

MoreSensorExamplesMeasuringStuff

Youdonothavepermissiontoaddcomments.

Signin | RecentSiteActivity | ReportAbuse | PrintPage | PoweredBy GoogleSites

https://sites.google.com/site/measuringstuff/moresensorexamples#TOCHSM20GHumidityandTemperatureMiniboard

18/18

You might also like