Sunteți pe pagina 1din 4

handson.dmt.fh-joanneum.

at

RASPBERRY PI
Workpackage: Description: Analogue Input with Capacitor (node.js) In this work package you can use a digital input to calculate analogue values. We use a light sensor for analogue input and a capacitor which enables us to get different values at different light conditions. 6 Node.js Code Understand the Code Test your Code Circuit Schematic Useful Resources Hardware: Raspberry Pi & Standard Equipment Capacitor (100uF) Light Sensor Straps Software: node.js node Libraries: onoff, sleep

Difficulty (1-10): Overview:

Requirements:

Carelse, Pauger, Pilz

Instructions

handson.dmt.fh-joanneum.at

ANALOGUE INPUT WITH CAPACITOR


Due to the fact that the Raspberry Pi has no analogue input we simulate an analogue input with the help of a capacitor. The capacitor acts like a bucket and the resistor (light sensor) like a thin pipe. To fill the bucket up with a thin pipe it takes some time. If there is much light the resistor (light sensor) is lower and so the capacitor is loaded faster. Depending on the light conditions the capacitor needs more or less time to load. So we get different analogue values. If you use the (node.js) code below you need a 100uF capacitor. For the python code, which you can find on http://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberrypi/overview you need a 1uF capacitor. This is because the python code is much faster and so the capacitor does not need such a high capacity. If the capacity is to low, the capacitor fills the bucket before the complete code is executed and so you will get no results (this is the case if you use a 1uF capacitor with node.js code)

Instructions
Create Circuit Schematic like described in picture 1 and 2. Be careful to connect the negative lead of the capacitor (shorter one, also marked with polarity band) to ground. After that you can write your code:

node.js Code
//require onoff to get access to gpios var gpio = require('onoff').Gpio; //require sleep to enable waiting for some time var sleep = require('sleep'); //function which always returns one value if the pin changes his value function RCtime(pin){ //variable for saving our own-created "analogue" value var reading = 0; //set pin to out and value to 0 var sensor = new gpio(pin, 'out'); sensor.write(0); //wait for 0.1 seconds sleep.usleep(100000); //set pin as input sensor = new gpio(pin, 'in', 'both'); //as long as the value of the pin is not reaching state 1 increase //value reading while (sensor.readSync() == false){ reading++; } //if value of pin is 1, return "analogue" value return reading;

};

while (true){ //print analogue value console.log(RCtime(18)); };

Understand the code


The while loop is adding 1 to the variable reading, as long as the value of the pin is digital 0. If there is much light (low resistor), the value of the pin changes faster to 1 and so the value
Carelse, Pauger, Pilz 2

Circuit Schematic

handson.dmt.fh-joanneum.at

of reading is lower. If it is very dark, it needs more time to fill the bucket (capacitor) and so the value is much higher.

Test your Code


Run your program by typing in: sudo node programname.js

Circuit Schematic

P ICTURE 1:C IRCUIT I NSTALLATION ON B READBOARD

P ICTURE 2: C IRCUIT D IAGRAM

Carelse, Pauger, Pilz

Trouble Shooting

handson.dmt.fh-joanneum.at

Trouble Shooting
If you have problems with importing/requiring the libraries, go to worksheet Setup Node.js on the Raspberry Pi and carefully read threw Install Libraries. If you have problems with running your application with sudo rights go to worksheet Setup Node.js on the Raspberry Pi and read threw quick2wire-gpio-admin.

Useful Resources
Library onoff https://github.com/fivdi/onoff Instruction with python code http://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi/overview All about Capacitors http://www.beavisaudio.com/techpages/Caps/

Carelse, Pauger, Pilz

S-ar putea să vă placă și