What would a sketch which allows current to flow (a switch) once a certain analog limit has been reached? Pretty much, how can you use Arduino as a switch, to switch on/off depending on sensor input.
NB
What would a sketch which allows current to flow (a switch) once a certain analog limit has been reached? Pretty much, how can you use Arduino as a switch, to switch on/off depending on sensor input.
NB
what you need is totally depending on what your switching, sensor, and when all this happens but the basic idea is
gather data from sensor (usually in digital format, but not always) when it reaches the threshold have the arduino switch a pin on, that triggers a transistor / fet / relay to switch the load
Osgeld is correct. For a basic analog input -> transistor setup, it'd look like this:
int input = 0; //analog input 0 is connected to the sensor
int output = 2; //digital pin 2 is connected to the transistor base
int threshold = 500; //the level at which the output will be triggered
void setup()
{
pinMode(output, OUTPUT); //initializes pin 2 as an output
}
void loop() {
if (analogRead(input) > threshold) //is the value higher than 500?
{
digitalWrite(output, HIGH); //if so, turn on the transistor
}
else
{
digitalWrite(output, LOW); //if not, turn off the transistor
}
delay(50); //just in case
}
DISCLAIMER: I have not tested that code.
Most likely you need some extra hardware to act as a switch. It depends on the current you want to switch.
http://www.thebox.myzen.co.uk/Workshop/Motors_1.html
Talks about motors but is the basis of switching anything.
An important feature is the hysteresis window
The concept is simple.
You set a threshold value e.g. of 512. When the analog input goes above that value, you turn the digital output high. When the value goes below, you turn the output low.
A problem arises when the analog input fluctuates around the threshold. This could be caused by signal noise or fast-changing environmental conditions (e.g. when the input signal comes from a light sensor).
As a result your digital output switches frequently from high to low and vice-versa. If that output controls for example a light or a motor, this can cause malfunctioning or even breakage.
To solve this problem you add an hysteresis window. That is, you create two thresholds, one for the HIGH to LOW transition, and one for the LOW to HIGH one.
Here's the modified code:
int input = 0; //analog input 0 is connected to the sensor
int output = 2; //digital pin 2 is connected to the transistor base
int threshold = 512; // nominal threshold
int hyst = 50; // on when input above threshold + hyst, off when below threshold - hyst
int samplingDelay = 500; // higher values for slow changing signals, lower values for faster response
void setup() {
pinMode(output, OUTPUT); //initializes pin 2 as an output
}
void loop() {
int v;
// read the value
v = analogRead(input);
// if it's higher than the on-threshold
if (v > (threshold + hyst)) {
// turn the output high
digitalWrite(output, HIGH);
}
// if is't lower than the off-threshold
else if (v < (threshold - hyst)) {
// turn the output low
digitalWrite(output, LOW);
}
// else don't change the output state
// wait before taking another sample
delay(samplingDelay);
}
HTH