I started making a project with arduino, then port it to a breadboard with an atmega328, and it's been a lot of fun for someone new like me.
I was wondering how I can add a USB port into my project to be able to put a lamp on it for example.
If I want to power off and on the lamp on demand, what kind of hardware would you recommend?
I was looking at USB to serial converter, but it looks more like a power in? Sorry if I'm wrong, I'm not really familiar with the vocabulary yet. I'm trying to achieve the inverse I think? power out on a big USB port.
The AVR chip can't host USB devices. It is like the lamp, not the PC the lamp plugs into.
The AVR can turn a relay on/off. You can power a real lamp with the relay.
The AVR can turn a transistor on/off to switch DC power to a big-bright led lamp. The AVR uses very small power to control the transistor (MOSFET is best efficient for this) which can take more power than the AVR chip much less one pin.
For a small led lamp you can connect leds and resistors to AVR pins and just turn the pins HIGH or LOW. Don't let any one pin source and/or sink more than 25-30mA continuous and try not to source/sink more than about 150mA total.
USB standard can power up to 500mA. The chip can't take that nor should it. It is good practice to minimize how much power goes through the chip.
Good (and still cheap) relay -modules- take very little power to control. A plain relay might (easily) need more than an Arduino pin can take to switch but the modules use external power to do the actual switching. Some have an opto-isolator, the AVR is electrically isolated from the relay as long as you don't ground them together (opposite of non-isolated wiring).
Do not turn a relay on and off a bunch of times quickly, it will get hot and may smoke on you.
Relays move physical parts quickly using magnet and coil. They have motor parts and make inductive surges but they are cheap and can make connections able to take a lot of power. Solid State relays (SSRs) are expensive compared, have no backsurge (the module deals with it) and can be switched at high frequency without getting hot.
You can get a relay shield. It's like a module only way more expensive.
I'll look into those Solid State Relay, I might need something that will switch it frequently (from a proximity sensor). Seeing the price right now, I'm hoping to find something cheaper
The sensor, you sense with a pin. The sketch watches it 1000x faster than human sense and the sketch can watch time, how long a thing goes on --- the sketch decides when the lamp changes, not the sensor.
A thing as simple as a button does not act so simple when the pin is read many times a millisecond. When the button is pressed, just before it makes contact there will be sparks jumping between the contacts. Even at 3V this is true, for any voltage there is a gap however small that it will jump. At 5V as the button goes down the sparks jump for about 2ms, on release the sparks jump even longer.
With fast code it looks like the button is pressed and let go several times, to Arduino it looks like the switch "bounces" and the ways to handle that are called "debouncing". One way to debounce is to watch the pin change and then keep detecting and once the pin reads the same for some time (debounce interval) the sketch sets a status variable to the new state.
The status variable is what the rest of the sketch uses as button state, not the state of the pin it is wired to.
Arduino can detect events and times. It can relate those as a bigger thing, an abstraction that gets used to decide when to act.
Automate with Arduino, but save interrupts for special needs only. The tutorials below are not to my work but they are GOOD.