Connecting LED via USB

Asked

Viewed 3,722 times

21

How can I send a 5V pulse through a USB?

Imagine a USB cable that has 4 wires (one generates 5V and the other negative pole). Left over the two central wires. My doubt is as I can read via Delphi that these cables are receiving 5V?

Goal: Capture signal for unit count via Serial.

Idea: Through a USB cable open an electric lock.

The program I’m developing would send a 5V pulse. The idea would be to connect this pulse to one of the USB cables.

Doubt:

  1. How can I send a 5v pulse via USB cable?
  2. With regards to sending: In C# how to turn on and off an LED via USB. Because direct connecting is easy using normal USB power.
  • Good question, I am also wanting to solve this. = X

  • 1

    I’ve long been looking for an answer to that question.

  • 1

    @Juliosantos has some code to facilitate a possible response?

  • Perhaps Steven Mcdowell’s (Prentice-Hall) book "USB Explained" can help you solve your problem. Although it does not pass source code, in it you find details about the specification of the USB protocol.

  • 1

    I understand this is a problem of electronics, not programming. And the programming part will depend on the type of chip that is connected to the USB, because you can not control consistently the USB as we did with parallel ports, because USB is a communication channel and not direct control - different from parallel ports that, in turn, already has the control electronics embedded in the specification.

  • A year after the previous comment I see that some answers, although correct, more complicated than they help. Extremely exaggerated to use an Arduino for a task that a parallel adapter of 10 real would solve. And the adapter comes particamente ready to use, just use a simple little drive to not pull excessive current from the parallel and ready.

Show 1 more comment

3 answers

9


Let’s go in pieces...

The USB (Universal Serial Bus) is a serial communication protocol used to carry out communication between peripherals, whereas a peripheral is composed of a "processing center" it is erroneous to perform the activation of an LED just by sending a signal via D+ and D-. Roughly the LED does not know how to read commands arriving via serial connection.

You can use electronic prototyping platforms, among them stands the Arduino.

Basically what you would do is develop and compile a code for Arduino where in this code you would receive command of your program (C#, Delphi, C++) via USB and activate the actuators like LED, Reles and etc...

You can learn more about:

http://www.arduino.cc/ and https://www.youtube.com/watch?v=kLd_JyvKV4Y

I have also available my course completion theme that shows a commercial development model with Arduino.

http://siaibib01.univali.br/pdf/Lucas%20Alves%20Selliach.pdf

An example:

Say you want to light an LED X period of time using an Arduino, you can use this code:

/ * 
   Acende um LED por um segundo, então fora por um segundo, repetidamente. 
   Este exemplo de código é de domínio público. 
* /

void setup () 
{
   // Inicializa o pino digital como uma saída.
   // Pin 13 tem um LED conectado na maioria das placas Arduino:
   pinMode (13, OUTPUT);
}

void loop () 
{
   digitalWrite (13, HIGH); / / set o LED
   delay (1000); / esperar / por um segundo
   digitalWrite (13, LOW); / / definir o off LED
   delay (1000); / esperar / por um segundo
}
  • Links may break in the future, post at least one example here please :)

6

Not why...

you can not do it in a "Beautiful" way, a solution would be to turn off and on the USB output, but I am almost sure that OS will not let you do it so easy.

USB is fully dedicated to passing information, but in the cable you find outputs +5V and GND to power the plugged devices.

Controlling an electronic device by programming

First you need to understand electronics, at least the minimum (current, resistance, etc.) you can use a microcontroller or a circuit capable of communicating with computer, nowadays the Arduous became the easiest and simplest project implementation platform for lay developers, Uino can be programmed in java,c,python being c the form adopted by the community.

Printing data in Serial output

int pushButton = 2;
void setup() {

  Serial.begin(9600);
  // informa que pino "2" é uma entrada de dados
  pinMode(pushButton, INPUT);
}

void loop() {
  // faz a leitura da saida "2" do dispositivo 
  int buttonState = digitalRead(pushButton);
  // Mostra isso na saida Serial
  Serial.println(buttonState);
  delay(1);
}

You can make your program communicate with others through the serial port.

Arduino communication with python serial port

Python is a language with many facilitations, there is module for almost anything, including for Rduino, web, interfaces...

#!/usr/bin/env python
"""
 Blinks an LED on digital pin 13
 in 1 second intervals
"""

from Arduino import Arduino
import time

board = Arduino('9600') #plugged in via USB, serial com at rate 9600
board.pinMode(13, "OUTPUT")

while True:
    board.digitalWrite(13, "LOW")
    time.sleep(1)
    board.digitalWrite(13, "HIGH")
    time.sleep(1)

Check out the documentation Python-Arduino

1

One other option is to use the Comx serial port as follows using pin 4 (DTR) in the former DOS would be mode Com1 dtr=off/on

db9 connector Pin 4=DTR Pin 5 GND Via software driving pin 4 connect resistor in series with led type 4---xxxx----led-----5 in the former DOS (window) Mode Com2 dtr=ON >>> turns on the led OFF >>> release the led

  • 3

    Hello, welcome to the site. Can you further detail the answer? It has a [Edit] link on it. Your idea is to plug the led directly into one of the serial pins? It has to key the 5V in this pin by software?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.