Hello, big-brained sexual dynamos of the Arduino forums!
I bring to you a problem that I'm 99% sure is going to be a simple fix, but I CAN NOT, FOR THE LIFE OF ME, figure it out.
I'm attempting to fire one of these laser diodes (650nm 5mw) with a button, using the below code.
int pinButton = 4; //the pin where we connect the button
int LASER = 12; //the pin we connect the LASER
void setup() {
pinMode(pinButton, INPUT); //set the button pin as INPUT
pinMode(LASER, OUTPUT); //set the LASER pin as OUTPUT
}
void loop() {
int stateButton = digitalRead(pinButton); //read the state of the button
if(stateButton == 1) { //if pressed
digitalWrite(LASER, HIGH); //write 1 or HIGH to LASER pin
} else { //if not pressed
digitalWrite(LASER, LOW); //write 0 or low to LASER pin
}
}
Seems pretty straightforward, but the problem is this: the diode lights up constantly, and turns OFF when you press the button...so, the exact opposite of what I want.
I've switched the HIGH and LOW states; all it does is make the laser slightly brighter when the button is pressed, whatever THAT means.
I've attached a photo showing my wiring. Let me know what else you need.
Thanks in advance, you magnificent, bio-luminescent seahorses.
A button and a laser diode, like most other electronic components, have 2 pins. The on/off state changes depending on whether the second pin is connected to Gnd or Vcc.
The code snippet
if(stateButton == 1) { //if pressed
digitalWrite(LASER, HIGH); //write 1 or HIGH to LASER pin
} else { //if not pressed
digitalWrite(LASER, LOW); //write 0 or low to LASER pin
}
could be made clearer and better readable with defined constants ButtonPressed and LedOn instead of 0/1 or HIGH/LOW.
And it can be simplified into
Thanks, but while the symptom (floating input) seems right, nothing in that post helped me. I added pull-up resistors, pull-down resistors, rerouted the button to VCC...nothing. The effect remains the same.
I DID notice that when I added a delay between the HIGH and LOW states, I had to hold the button down for that amount of time before the "LOW" state would activate.
Not sure how that's helpful, but it feels important.
The power requirements depend on the laser diode type. Those cheap laser pointer(?) diodes are designed with a built-in resistor for 5V and 20mA, with additional current limiting resistor for less power, and higher voltage (tested 4.5-7V) for more power.
The used LASER=12 pin is fine. Pin 13 is already loaded with circuitry for the on-board LED.
Something about your wiring or maybe your laser did not come with a constant-current circuit like mine did.. in bags of 10 for less than $5 as 4.5V (works fine on 5V) 20mA --> 5mW laser modules.
Take extreme care where the beam goes, the reflections can blind eyes too. A buddy of mine caught his beam off a curved mirror and for about two full hours he wasn't sure that that one eye wouldn't see red permanently.
I wire pin to button to ground then set the pin mode to INPUT_PULLUP that supplies the pin with 5V through at least 20K resistance which is a lot. When the button is up, the weak 5V raises the pin HIGH. When the button is down the connection to ground drains the 5V as fast as it is supplied. The current Amps = Volts / Ohms = 5 / 20000 = 0.00025A = 0.25mA at most (resistance may be 20K to 50K Ohms) which is pin-safe low power.
Note that pin HIGH is switch OPEN/OFF/UP and pin LOW is switch CLOSED/ON/DOWN.
HIGH is button UP, LOW is button DOWN, can you work with that? Then there's just the matter of bounce....
The subjects linked to below will answer a lot you have and haven't yet asked.
Knowing basic DC electricity really helps. Get down to charges in wire level as what happens with Arduino goes there.
4Pcs 4.5V 12mm Red Laser Head Module Plastic Laser Diode Laser Dot P0.05 Pi WDC
Lasers are good for coherent wave tricks like with interference patterns. Out in front of the slits looking back, lights appear to be projected. In school class we measured the apparent distance and worked out the frequency of our monochrome (but not laser) filtered light. It'd just look better with laser light.
OTOH a 20mA red led spews magnitudes more light than a 20mA red laser. It may throw brighter interference patterns.
Okay, it's been a while on this one (I had a baby!), but I'm still doing this.
Here's the current problem; the diode stays ON unless the button is pressed. What I need is for the diode to stay OFF until the button is pressed, turn ON for an adjustable length of time, and then turn back OFF.
No matter what changes I make to the code, I can't seem to get it to work. Am I missing something, or is it a hardware thing?
Nevermind; I'm a huge dummy. Sent the B-side of the button to GND, added a delay in the code (see below), and it works like a charm. Now I just need to tweak the delay to meet my needs.
int pinButton = 4; //the pin where we connect the button
int LASER = 7; //the pin we connect the laser
void setup() {
pinMode(pinButton, INPUT_PULLUP); //set the button pin as INPUT
pinMode(LASER, OUTPUT); //set the LASER pin as OUTPUT
}
void loop() {
int stateButton = digitalRead(pinButton); //read the state of the button
if(stateButton == 1) { //if pressed
digitalWrite(LASER, LOW); //write 1 or HIGH to LASER pin
} else { //if not pressed
digitalWrite(LASER, HIGH); //write 0 or low to LASER pin
delay(500);
}
}
I'll be building this circuit into an old-timey rifle and making a laser-based shooting gallery out of it. Thanks a million for all your help, folks, as always.