General
Arduino detectors
Arduino detectors
A sensor is a device that according it's environment, can change a physical characteristic. This can then be converted to a scale and compared to a standard. A simple thermometer has mercury in a thin tube that expands and contracts according to the heat content of the liquid. a simple standard is to compare it with the freezing point of water (0o C) and boiling point (100o C). Humans a probable best suited to live around 25o C. As the microcontroller is a very simple device that can process electrical signals. For an Arduino to sense light, temperature, or other physical quantities, it needs something that can convert them into a voltage. That is a sensor. Sensors then trigger an actuator. Once the sensors have been read, the device has the data needed to decide how to react. The decision-making process is handled by the microcontroller, and the reaction is performed by actuators. These functions could be performed by a light or an electric motor |
||
A feedback loop uses sensors and actuators to control a process. It is the part section of a control system which allows for feedback and self-correction while adjusting its operation according to the difference between the actual output and the desired output. There are many examples of such loops. A washing machine stops filling when it has enough water, aircraft on autopilot changes it's flight or someone breathes harder when they exercise. |
||
A sensor changes its properties (eg resistance) according to it's environment. An actuator (eg a motor) changes in response to the detector. |
An anolog signal is continuous over time and can take on any value in a range. | ||
Arduino's have an analogue to digital converter built in to the chip. The analogue inputs have the letter "A" in front of the input. | ||
![]() |
Sparkfun's explaination | |
A very simple demonstration of how an analog sensor works. It is presented as written. |
||
// Ultra simple thermometer // by Leon Harris // Version 0.1 /* This thermometer is based around the fact that silicon diodes have a temperature coefficient of about -2mv /0C. So if supplied with a constant current source, the voltage across a diode will decrease by -2mV for every degree centigrade. All you need to do is to supply it with a constant current source. In ARduino terms, this means a resistor from a regulated power supply to inject 0.5 to 1mA. I am to cheap to buy a resistor, so I have instead programmed one of the internal 10K pull up resistors on Analog input 0, and simple wired the anode of the diode to A0, and the cathode to A1, which I set to digital mode and to output 0v (ie connect the diode to ground). Thus and Arduino plus 1 part can make a thermometer! */ int Temperature=0; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(A0,INPUT_PULLUP); pinMode(A1,OUTPUT); digitalWrite(A1,LOW); } void loop() { // put your main code here, to run repeatedly: delay(1000); Temperature=analogRead(A0); Serial.println(Temperature); } |
![]() |
|
This exercise reads the middle pin to the analogue 0 pin directly. There is no need to manipulate the voltage using a voltage dividers, reading directly does the trick. Use the serial window in your Arduino sketch application (Tools -> Serial Port) to see the results. Note the use of read and readln to format the results. Using the concatenation + operator and the string "," later on means you can create output that can be read direct to a .csv file that can be read directly into a spreadsheet. Also not this sensor reads in Centigrade, and the neat conversion formula to change it to Fahrenheit (for Americans). Check the website for a discussion on analogReference (ahref). |
LM 35 reference | |
/* code to read a centigrade sensor ln35dz and report int analogPin = 0; void setup() { void loop() { |
![]() |
|
/* using ahref to get a more accurate reading */ float tempC; int reading; int tempPin = 0; void setup() { analogReference(INTERNAL); Serial.begin(9600); } void loop() { reading = analogRead(tempPin); tempC = reading / 9.31; Serial.println(tempC); delay(1000); } |
Write to the serial window, cut and paste into a spreadsheet. | ||
/* this code reads a light dependant resistor and then prints it out in the serial window of the Arduino SDK mweber 290414 */ int reading1; //connected to analogue pin 01 void setup() { Serial.begin(9600); // this communicates //with the serial window pinMode(A0, INPUT); } void loop() { reading1 = analogRead(A0); delay(5); Serial.print("sensor =>, ");// note print // note comma Serial.println(reading1); // and println delay(500); } |
/* instructions at https://www.youtube.com/watch?v=1GD29sXLOJ0 */ #include <OneWire.h> // Data wire is plugged into port 2 on the Arduino // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) // Pass our oneWire reference to Dallas Temperature. void setup(void) void loop(void) |
||
/* This sketch will display a reading in degrees centigrade on a Nokia 5110 screen pinouts nokia 5111 code from https://www.youtube.com/watch?v=ga_1dqGjsbo hacked by numbat mark */
#define ONE_WIRE_BUS 2 // it reads the digital pin LCD5110 lcd(3,4,5,6,7); extern unsigned char SmallFont[]; char temperatureF[6]; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); float tempC = 0; void setup(void) void loop(void) lcd.clrScr(); void convertToString(float number) |
![]() |
||
![]() |
/* #define trigPin 13 void setup() { void loop() { |
|
TCRT5000 |
d | |
![]() |
//this is a program to test if we can read a sensor int sensor; // use an integer and read it continually void setup() void loop() |
Data sheet |
The DHT11 is a very low-power device – it consumes no more than 2.5-milliamps (2.5mA) of current. The digital I/O (input/output) ports of an Arduino can supply up to 40mA, so if we set the Arduino’s D3 pin high (that is, to 5V) and the D6 pin to GND (0V), we can power a device between those two pins, up to 40mA. The DHT11 can measure temperature from 0 to 50-degreesC and 20-90% relative humidity (the DHT22, also called the AM2302, can go from -40 to 80-degreesC and 0-100% relative humidity). But to read the data from sensor’s Pin 2, we need to apply a five-kiloohm (5kO) resistor connecting that pin to the 5V supply line or ‘rail’ – in this instance, it’s called a ‘pull-up resistor’ because it’s ‘pulling up’ the sensor’s Pin 2 to the supply rail. But this is where we’ll use another hidden trick – the Arduino Uno’s I/O pins all have built-in pull-up resistors and we activate the one on Pin D4 using one line of code: pinMode(4, INPUT_PULLUP); That tells the Arduino we want Pin D4 as an input (so we can read data from the sensor) and to activate its internal pull-up resistor. Pretty sneaky, huh? The upshot of all this is we’ve made it incredibly easy to install the DHT11 into the Arduino – all we need to do is just plug it in! With the DHT11 label facing in, it plugs into pins D3 to D6. Grab the zip file from our webpage atwww.apcmag.com/magstuff. Download it, unzip it and copy the contents of the libraries subfolder into the libraries folder of the Arduino IDE. Restart the IDE and load in the ab02_temphumid_v1.ino sketch file. Remove the DHT11 sensor if you have it plugged into the Arduino board, then plug the board into your computer via the USB cable. (If using a Freetronics Eleven board, get the driver software first fromhttp://tinyurl.com/elevenusb.) In the IDE menu, select ‘Tools’, ‘Board’, choose ‘Arduino Uno’, then select ‘Tools’ again, but this time, go ‘Serial Port’ and select the COM port for your board (it won’t likely be COM1 – that’s your mouse!). |
|
|
/* Arduino Basics - #2 - DHT11 temp/humidity sensor #include <dht.h> void setup() { |
||