Do Any Members Have Skill or Experience With Arduino; Coding for Microcontrollers

Fantastic news!

So is it still happening or are you trying to sort out what was happening earlier?

My guess is that pin flashes before the initialization code runs and is part of the base firmware, that runs underneath the code you write and send to the Arduino. It may be happening a few times as the reset button bounces. When you press a mechanical contact button the contacts electrically “bounce” or flicker so to speak. Different buttons have different behavior, and there’s usually a capacitor on the electrical reset line to avoid this sort of behavior, but that may not be doing the trick quite well enough. Google “button bounce” for more info

Glad you are finally getting some tangible success :+1:

1 Like

Adafruit version of software debounce. Debouncing | Make It Switch | Adafruit Learning System

1 Like

I was just pointing out that this “bouncing” (spazzing) solenoid behavior never happened/happens when plugging in the power, but only when using the reset button. At least I don’t recall if it ever happened when plugging the arduino in…

So, I think it must be something to do specifically with pin 13…

This sounds like a reasonable explanation for what might be happening.

That’s a term I was trying to remember a few days ago when posting here; “debounce”/“bouncing”. I was going to ask about that being a possibility, but couldn’t remember the term or perhaps couldn’t remember if it was the right term. I also thought, maybe the behavior had something to do with my diode, and current “flowing backwards” or something like that. I was likely confusing the two.
I’ll check that out. Thanks for the link.

1 Like

Trying to decide a simple, easy to work with platform to upload a short clip. Or I could upload to a good file hosting site where one could quickly and safely download the short clip. Any recommendations on the former? I know of several but haven’t decided and would be interested if anyone has an opinion or used them. (I’m talking other than youtube).

Edit:
Oh, and also…I’m curious how to go about ‘refining’ this now. I have questions about certain things.

  1. Regarding the setup I’m using at least, why is a 12v solenoid not functioning the way I think it should? It seems a bit weak and/or slower than the 5v solenoid I’m using (which should be actually seeing 12v ish)? I’ll have to look around for some tiny probe tips for my DMM (I think I have some somewhere, but I might’ve soldered some very small gauge wire to the tip of bigger probe tips when I last attempted to do some measuring on this circuit and attemped to make sense of them…).

  2. In RTC’s trap build (with his code, and his chosen components) the only components I know for sure he used are: 1) Arduino pro mini clone. 2) 12v milwaukee battery as power source. 3) 12v Solenoid - I don’t know which one exactly, but I’m quite sure it’s a 12v, and he might have said as much - he probably did. 4) IR break beam - I seem to recall him saying he used the adafruit set. I would think he used the 3mm led version, not the 5mm. Because the 5mm isn’t needed, for one. And because I sought to buy the exact same ones he used, from what I remember. And I got the 3mm adafruit ones.

  3. In RTC’s trap build, I don’t think I was given any info on other components like a transistor/mosfet, a diode, etc. And judging from the pictures/still I took from the two videos of his I save, which are not high res./clear, I can’t see if there are any other components being used.
    -I just realized, I don’t think I uploaded any of those stills of his trap. I will do that after asap.

Oh, I did. Post #33. Here’s one with a better view of arduino pro mini clone, and milwaukee 12v battery.

Rat Trap Chronicles - ScreenCapture

Quick video clip of a test of the trap’s function. It’s a file upload/hosting site. That’s what I’ve got for now.

I’ll type up some advice tomorrow Nitt, hadn’t noticed your posts :+1:

1 Like

No sweat, FE. Cool, thanks.

Using a darlington is a not-so-great switch device like I’ve mentioned before. I’d use something like IRLZ44, which is a common N-MOSFET with logic-level gate. You can find it on Digikey.ca or probably Amazon or whatever is best. It’s a ubiquitous part. The MOSFET has very low-loss, so you get all your available voltage into the solenoid. The darlington, even when fully “ON” presents a fairly large voltage drop, and requires some base current. This is just not the best tool for the job nowadays (and for the last 30 years) - like using a framing hammer for trim work, just because the framing hammer was invented first.

The 5V solenoid you linked is about 4.5 ohms. When you run that from 12V, you get I=V/R=12/4.5=2.67A. That’s significant: P=IV=2.6712=32W. 32W to trigger a mousetrap seems excessive to me, but viable. Note, the solenoid is “rated” for 5V. That will be thermally-limited, meaning it can run from 5V indefinately and not burn up. How much power is it dissipating at it’s 5V rating, let’s find out, P=I*V=(V/R)V=(V^2)/R=(55)/4.5=5.56W. See how big of a difference that extra voltage makes? It’s about 6x over it’s power dissipation rating (basically how much power you can apply and it gets rid of safely), and it’ll melt after a while. So, the solution you have, could not be left in an actuated state for long, and you aren’t, it’s just pulsed on. Running though these numbers with you now, I’d recommend you make that solenoid ON pulse even shorter, maybe 100ms.

I can’t find a link for the 12V solenoid you mention, but my expectation is that its action is less vigorous just because you aren’t driving as much current through it. I speak of current, rather than voltage because magnetic “force” is proportional to current - magnetic field is proportional to current. Voltage is related to current but it’s like using a stove’s burner setting to relate to how hot fast food cooks - yeah, there’s a relationship, but it’s the temperature of the pan that cooks the food, and it depends. Better just measure the temperature of the pan, right? Maybe thats a bad analogy but seeing your interest in other electronic stuff and homebrew lighting I want to communicate a bit more information than you are directly asking for.

I’d just use what you have, it won’t matter as long as you keep the pulse short.

I think RTC used the pro mini arduino to reduce power consumption and extend battery life. There’s some complication to doing this that may not be worth it at this point in your DIY career - namely that you need to program the microcontroller seperately. It doesn’t just plug into your computer USB and press a button in the Arduino IDE to program it. People seem to like the Nano for low-power stuff, but they remove the LEDs on the board that are several mA each. I also see the ESP32 recommended, which can use the Arduino environment. I’d use the Nano or a Micro just because I have some laying around. The Micro seems to be fairly low-power and easy. The Pro Mini requires an FTDI adapter, which is also readily attainable if you are willing, I found several tutorials like this one: https://www.arrow.com/en/research-and-events/articles/getting-started-with-the-arduino-pro-mini That’s the way you should go if you want the longest possible battery life.

What battery do you intend to use? There’s no requirement for a specific one (I wouldn’t buy Milwalkee batteries just for this project), what do you have lying around? I think you’ll get away with anything between 7 and 18V here, and we can modify the circuit or solenoid to accomodate almost anything. The battery you pick will determine how important it is to truely minimize the power of the arduino solution.

Like I detailed above, the solenoid itself just has to mechanically actuate the trap. I think almost anything will work here. Same with the break-beam sensor, what you have works, I’d stick with that.


In summary, you have a lot of flexibility here. You don’t need exactly the same parts to create the same effect. I’d worry about picking a battery you have available easily and inexpensively, we can pick the rest of the stuff based on that.

1 Like

A note on battery selection and power consumption.

Your break-beam sensor consumes about 20mA total when powered. A standard Arduino consumes about 40mA when normally running. You test setup therefore, is generally consuming about 60mA.

RTC switches the break-beam sensor off most of the time so you draw that power only for a short period of time. He also sleeps the microcontroller, so it draws much less than 40mA it normally might. He’s showing in his code comments that the average current is something like 0.5mA. Which is a fantastic improvement over 60mA, 120x longer battery life.

Let’s say you have a 2Ah battery, or 2000mAh. The time this battery should run something that draws 60mA is: T=Capacity/Current=2000/60 or 33 hours. If you are drawing only 0.5mA, that becomes 2000/0.5 = 4000 hours (this is 166 days).

You’ll also note in his code that when he’s using the IR camera he just leaves the sensor LED on all the time, so he’s looking at more realistically about 15mA, or 2000/15 = 133 hours or 5.5 days.

Anyway, that’s how the power consumption impacts battery life/battery selection. If you use a car battery, they average 50-70Ah (50000 to 70000mAh), you could use an RC battery of a few thousand mAh, you really just pick what you have available and we can pick how aggressive you need to be with power consumption minimization.

1 Like

Gotcha. I’ll order a couple mosfets. You linked some earlier in the thread IIRC.

Understood. And yea, 32W just sounds like a lot.

Oh, ok. I was thinking the 5V “rating” might have meant that 5V was the minimum voltage required to even actuate the solenoid. And that the “usable” or “safe” range might be something like 5V to 10V or something (I might have seen something in the datasheet, I’ll have to check).

The math itself is simple enough, but I lost you a little bit somewhere along the “flow” of the variables or manipulating of the formula here. I underlined it above. (Please see my Note 1 at very bottom).

Do you mean the voltage “drop” over the solenoid itself (as far as calculating it goes)? I’ll try and draw it out and see if I can see how you got that number.
Ok, makes sense - I think I had it set to 350ms originally, but only because that seemed like enough, and not too much (to push/kick rat trap’s bar, not due to heat or anything though).

That’s what I guessed; not “enough” current. (Power adapter is 12V/2A). This is the 12V solenoid (I have two of these): LINK. I linked it earlier in the thread. There’s a datasheet, a ‘driver diagram’ (labeled “how to drive solenoids with arduino”, and a diagram for the actual part/dimensions etc. on the product page. Also a note saying “12 VDC operation (please note lower voltage results in weaker/slower operation) - ~300 mA current draw”, as well as a revision note saying “As of Jan 17, 2018 we’re selling this solenoid with a 12VDC actuation voltage instead of 24V”. I mentioned this revision note earlier, because I thought to myself "did they actually have the solenoid (coil?) altered/modified? Like the number of turns/windings or something? One would think so…?

Should one of the instances of the word “current” in the underlined text above say “voltage” instead?
It’s probably a good analogy which I’ll fully get once read a couple more times. Thanks.
On that topic, I hate that a stove top has Min. through Max. or Min, 2, 3, 4, etc., Max. I use an ir thermometer and learned the correct “number” to put it on now, combined with the Maillard Effect/Reaction using the water drop test to verify the correct temp. (for searing meat, and frying stuff, more or less). Not ideal. Haha. Anyways…


I’ll respond to the power consumption stuff next. Thank you.

Note 1:
I school, we were given the “Power Wheel” formulae cheat sheet. I obsessed over this, learning it, and learning to rearrange formulae so that rather than remembering something like twelve formulae - eight of which were just alternate forms of the other four - I could just remember/know the four I needed, and understand how to rearrange/manipulate these four to get what I needed. Here’s basically what I came up with (This is all the formulas we used, only the top three are the ones from the power wheel, IIRC):
Electrical Formulas.pdf (29.0 KB)
Oh - it’s pdf so it won’t display as an image. Anyways somewhere along the way I lost you in the “=” symbols and don’t know if you were “next” or “equals”. I used to do something like this:
P=IE → P=2*4 → P=8. Where the “–>” meant “then” or “next”. IOT, trying not to skip steps so as to better learn, and to make sure I didn’t lose “points” on a test, etc. Haha.

Sorry to waste your time with this explanation, hah!

Note: Regarding the 2.67A from the first calculation above. The 12V power adapter has a max of 2A. How would that affect these numbers and the actual circuit I’ve got setup right now?

A day or two ago, I found my DMM probes with the nice small tips. Still too big for breadboard etc. And the pos. probe has a wonky connection near the conductor connection. So I took it apart and it was a broken solder joint (these are my cheap probes). I resoldered it and it’s good now (apart from the now missing insulation, haha). Then I used some Male to Fem breaboard sized cables and fit the small probe tips into the F side. Now I can use the M side to measure stuff in the breadboard header without ruining the holes.

I also measured my two solenoids, after looking at their resistance value on them, and knowing I knew the voltage, thinking “I could probably get some real values here”. Anyway I ended up just checking the Resistance on the them. The 5V read 5.1Ohm, the 12V read 40.8Ohm. DMM used is a Brymen BM235.

Before you read this whole thing, read the last few sentences. Don’t get too bogged down in minutia if you don’t have to, but you are welcome to dive in with me. Just be aware much of this reply may be totally unrequired.

Referring to your chart/formula sheet E=voltage that I call V because I’ve done it that way forever.

So to go through this in order:

  1. Power = Current * Voltage
  2. Current = Voltage / Resistance
  3. I’m inserting line 2 into my “current” variable in line 1, or Power = (Voltage^2) / Resistance and solving: (5V * 5V) / 4.5 ohms. I just realized that when I put the asterisk between two numbers it displays as italic with the asterisk missing. This is probably the confusion and I’ll try to avoid this syntax going forward.

Yes, I mean the voltage across the solenoid when it’s actuated. I’m assuming the switch transistor turns into a short-circuit (which is almost true in the case of the MOSFET) when you apply a signal from the Arduino.

I had changed it to 500ms because it wasn’t an unreasonable number to try. It’s very likely anything longer than about 50ms would be enough.

—This may be excessive information—

There’s ways to calculate this, of course, but if you want to do that I’m going to have to start scanning notebook pages to walk through it, typing it here in detail will give me a stroke. What’s the path of that calculation? Ok, so what do we know? We know initial actuation force, the stroke, the current we’ll eventually be applying and ideally we’d make an accurate guess of plunger mass + spring rate. There’s another time-response, the current doesn’t immediately appear because of electrical time constant (it has inductance), but we can’t see that in the datasheet so we’d have to guess, measure or assume it negligible [dubious assumption at best]. Magnetic force is proportional to B (magnetic field intensity), B is proportional to N * I (number of turns * current). If we know the mass of the plunger, the return force as a function of position based on the spring, we could, after some integration from acceleration->velocity->position solve for the total transit time. This is something I might do for a very sensitive application, and have done similar computation for very fast optical focus systems. The key there is that F = m * a (thanks Newton!). You can calculate F as a function of time [we’d call this F(t)] from B as a function of I [B(t)]. Now we can find our plunger acceleration as a function of time (once we’ve subtracted the return spring contribution in the negative direction). We’d integrate that over the distance of plunger travel to obtain a time interval from initial voltage application to solenoid slamming shut. It isn’t too bad, but computing it is not something usually done. It’s probably only neurotic engineers and scientists that would bother with this, as I probably am neurotic and have actually executed this series of calculations for a particular application where the response mattered.

Maybe it’s better to just say the engineering approach to any problem is this:

  1. Decide if we need a better answer to our problem
  2. List the things we know (mechanical and electrical properties of solenoid)
  3. List what we are trying to find
  4. List relationships between the domains (Force = mass * acceleration, position = integral (velocity = integral (acceleration)), Force = constant * current * some position sensitivity)
  5. List the things we still don’t have to complete our equations (spring rate, plunger mass, electrical inductance)
  6. Make measurements/assumptions about those items in step 4
  7. Develop a path to a solution (in this example Force is our cross-domain parameter, F=F or mass * acceleration = constant * current)
  8. Finalize and simply equations
  9. Execute computation

Contemplating deleting that because maybe that’s just confusing AF.

—End of excessive information blurb—

Yes, almost certainly what they mean there is they are carrying only the 12V winding version. It’s optimized for 12V operation, but it will never work as well as your over-driven 5V part for this application, because you are putting less power into it. Less power, less force.

As far as your 12V/2A adapter goes, you may be totally getting away with your 2.67A 5V solenoid current. An oscilloscope or fast multimeter would help you discover if that’s a problem or not. But I think you’re fine there.

No, I meant to say current. Let’s think about magnetic force, it’s proportional to magnetic field strength, which is proportional to current. Electromagnet - Wikipedia

You certainly aren’t wasting my time, I may be sending you into a rabbit hole you don’t need to dive down. Again, I’m always willing to help anyone that wants to learn, but I don’t want to bog you down in unneccessary detail either. Many of these conclusions I jump to are the result of many years of experience with numerous systems and relatively safe assumptions. As I’m learning at work mentoring other engineers; I have to be very careful not to provide excessive amounts of information until those questions naturally develop, and someone asks “why?” The long and the short of it, I’d continue using your 5V solenoid, and over-driving it, but keep it on for as short a period of time as works. You can play with it. Start with say 10ms, see if it works. If it doesn’t, go to 20ms. If that doesn’t, try 50ms. And so on. Just be aware, the problem is fairly complicated and multi-variate.

Those values of resistance are consistent with the specifications, at least within 10% or so. That’s good.

In conclusion, I’d just use the 5V solenoid, but keep it quick. If you want to check your adapter make sure it doesn’t drop much below 12V when the solenoid fires.

1 Like

much of my electronic experience has been thinking of an idea of something cool i want to do, spend a few hours in the garage and storage building getting distracted by the other cool half-finished projects lying around while i search for material to use and refine the idea in my head (after a bong hit), then realize that i need to learn how to do anywhere from one to five new things in order to finish the project, spend up to a year and a half learning said things in a passable fashion in much the same way being done here, and then actually starting said project.

from here it can go one of two ways, the second one being most common. 1. spend months of frustration before finally getting it. 2. spend months in frustration only to lose interest, need for idea, or money and/or time and set project aside in garage or storage shed only to be finished later, scavenged for parts for next project, or wither away until someone has to go through my things when i pass and donate them or have a yard sale.

this was the epitome of a proper learning experience. op comes in with a wonderful attitude, finds help from several helpful individuals, field expert comes in spending hours assisting while half-trained pothead politely steps back and learns almost as much or more, op gets his project working and learns a ton. now i want to make one and have everything lying around. i love this place, you guys all rock!

oh, and @Nitt , don’t worry about participating in a forum for this, you got this down in spades. as you found out, folks love helping someone who is learning and putting in the effort.

2 Likes

I think what makes this whole thing so beautiful is that finally here I get to contribute, after taking so much in terms of new friends and growing knowledge.

My outdoor grow thread is full of my questions, and other people helping me along. Same deal, different subject. Life is all about giving when you have it, and taking when you need it. Full-circle fun.

Happy to see @Nitt getting progress after so much effort.

1 Like

You don’t need to respond to everything, some of it is just me replying if for nothing else than to show that I am reading, and re-reading and considering your notes and advice.

It’s hard not to want to, for me. I want to dig out whatever text books I have left and hunt for notes in binders to “remember”, but I just don’t have the time.

I think you’re saying that you used one formula to solve for a varible, which you then use in another formula to solve for a different variable, but I don’t see it. If I just ignore my inability to see it (which hurts), and use the formula (the P = V^2 / R formula), knowing that it’s just the rated voltage (5V) of the solenoid and the rated resistance (4.5 ohm) being used, then I see the 5.56W power that the solenoid should be seeing/dissipating.

I had tried to measure this using DMM, but the results were quite different each time. I’m sure you know why, my guess was partly that it’s too quick of an action to measure/display. I played with the “average/min/max” function on the DMM to see how that worked (never used it) and maybe that would actually work. Anyways…

It’s very involved, for sure.

I also considered or wonderd if/when the tiny jumper wires would be a limitation in the circuit/to the current.

I have a microcontoller based trap that uses a 12v adapter (with optional AA battery pack adapter) and what appears to me to be the same 5v solenoid that I’m using. On AA battery power, it’s… a bit too weak to function the way it’s designed too. Though it’s not that much better on the wall adapter. I’d say it’s just barely enough force/power. But if you factor in cold temps, and/or low AA batteries, it’s just not good enough. I immediately had to modify a non-electrical related part to try and get consistent strikes that would release the “kill bar”.

Ok, just checking. It sounded like it might be one of those sentences where one compares/contrasts two things or opposing things.

Ok, I’ll try this.

And I’ll try this.

How do the IR sensor, and the microcontroller know when to “wake up”?

Are you saying that the IR camera (you talking about the camera used to film, right?) is somehow worked into his trap function - as in it plays a part in “waking up” the trap (the break-beam and the microcontroller)? Or maybe I’m misreading that.

All I can think of that I have on hand are five Samsung INR 18650-20R, 2000mAh, batteries from an 18V power tool battery. They’re old though, and have not been maintained or charged and are still spot welded together and all that. I kinda doubt they’re still viable.
I was thinking of using 18650s though, and I thought about buying a spot welder (no way I’m playing with a microwave oven transformer at this stage, haha!) to use 18650s. But then I thought of these as an option for 18650s: LINK.

I really want/need the trap to be suitable for outdoors; cold temps. And having to plug it in won’t be very realistic, certainly not ideal. I’d want to get no less than a month of battery life out of it, but that’s not based on an understanding of how attainable or realistic the possibility is. It’s based on convenience, and the likelihood that a neighbor or myself is going to go out to the trap location and change batteries every 24-48 hours.

I don’t mind buying whatever battery power source would best suit this need; for both having enough power to actuate a 5V or 12V solenoid, and for giving the longest possible duration as reasonably attainable.

It sounds like this is one of the more complicated aspects of this all though…

Thanks.

1 Like

I could not agree more, and I relate to this a lot. Though I have less experience with this specific topic than you guys (much less).

Yes. I’m greatful. Amazing.

Thanks man. I appreciate it.

you’re welcome. and you don’t need a battery, although the ones you have will work fine but you may find a dead cell or two, you can just plug it in. i’ve also used a battery pack made for charging in a camera hidden in a coffee can that caught some thieves. that was a cool project for a raspberry pi, the camera module was taped to the inside of the coffee can and the hole was so tiny and strategically placed it was nearly invisible. it had wifi and was streaming video to a computer hard drive, and he could watch it from his phone. i also tried to make a battery spot welder, but i cut into the primary coils accidentally. i’ve got two other ones though to finish but other things took priority.

1 Like

I’m guessing you are having difficulty because the pulse is so short. Depending on your multimeter, most don’t respond well to <0.5 second blips. It’s a fair and likely accurate assumption that you are getting 12V across your solenoid.

The other stuff has measureable impact, but is not important enough to worry about - at least I don’t think so.

It’s on a timer. Basically, he turns the parts that draw much power, the break beam sensor and the microcontroller off most of the time. The break beam sensor gets “powered” from one the IO pins (PC0). He turns it on just before reading it, waits 40us to let it start working properly, reads it and shuts it off again. His code is ~40us on, and off/sleeping for 60ms. So the long-term current consumption of the circuit is essentially the fraction of the time it’s on. 40us/60ms = 0.000666, or 0.07%. If you have an active sensor-running consumption of, say, 40mA or so in your case, there is potential to drop that to 40mA * 0.07% = 27uA To put that into context, let’s say you have a 12V 2000mAh cell. Depending on some other circuitry, that could last 2Ah/27u =75,000 hours. That’s 8.5 years.

UNFORTUNATELY, some realities come into play here. The microcontroller requires some power even while sleeping because it basically sets it’s “alarm clock” to wake it up when it “naps.” That power is stated to be 464uA by RTC in his code. What do we notice about that 464uA compared to our sensor at an “average” current of only 27uA? That’s right - it’s bigger, ~20X bigger.

Here you enter a place where you could take this design anywhere you want to. If it were me, I would try to design something that takes 2AA batteries or so, and use a charged capacitor to trigger the solenoid and an ultra-low power microcontroller that would consume less than about 10uA in sleep mode. I know I could target somewhere around 40uA of average standby current, and actually a little less because I would design my own break-beam sensor. BUT: Thats a whole other can of worms. Or cans even. I’d put it all on a small circuit board and probably post 'em online and hope people want the extra ones.

OR: What I would do at your point in the process is just evolve slightly - move to the Pro Mini, or if you are willing to undergo a more significant learning challenge, NUCLEO-L412KB STMicroelectronics | Development Boards, Kits, Programmers | DigiKey? That’s a route that would take you to 10x longer battery life than RTC could get. The reason this is a steeper curve is the level of sophistication and programming is next-level compared to Arduino. It’s 0.5mm mechanical pencil compared to a marker. From a top level anyway.

If you go the Arduino Pro Mini route, it’s going to be an easier transition and you’ll get by with 2-3 18650s in series. I don’t see anything wrong with those spring battery holders, that’s the safest approach unless you have really good soldering equipment and skills. Chargers are cheap and you can charge them and have them around. I’d say this is your next step - migrate your design to the Pro Mini. Read the tutorials and get the FTDI adapter cord for programming.

I certainly wouldn’t be worrying about a spot welder.

Cheers man, happy to help you along. However I can.

2 Likes