Beruflich Dokumente
Kultur Dokumente
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
Requirements:
Instructions
handson.dmt.fh-joanneum.at
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;
};
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.
Circuit Schematic
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/