Controle Lego Power Functions com Arduino e Bluetooth – Guia Mestre
Componentes e Materiais
| Componente | Qtd | Nota |
|---|---|---|
| Arduino UNO | 1 | |
| Driver H-Bridge Dual L293D (Texas Instruments) | 1 | Versão DIP (pinos longos), não SMD |
| Protoboard | 1 | |
| Conector 9V → Jack Barril | 1 | |
| Fio Extensão Lego Power Functions (Item 8886) | 3 | Cortar para fazer breakout |
| Módulo Bluetooth HC-05 | 1 |
Sobre o Projeto
Controlar modelos Lego Power Functions (motores DC, servo) com Arduino e app Android próprio (MIT App Inventor). Exemplo: carro 4x4 Crawler 9398 com 2 motores L (tração) e 1 servo (direção).
Entendendo a Fiação Power Functions
Compre fios de extensão (item 8886), corte e faça cabos breakout (pinos de um lado, plug Lego do outro).
- GND = negativo da bateria (preto)
- C1, C2 = controle de polaridade (inverte direção motor/servo)
Plug cinza claro (fêmea) para servo (4 fios) e motor (C1, C2). Plug cinza escuro (macho) para bateria (9V + GND).
Controlando Motor Lego com L293D
PWM para velocidade/direção. Arduino: 5V, 30 mA; Motores Lego: 9V, >100 mA. L293D = 2 H-bridges em 1 chip → lado A: motores (paralelos), lado B: servo.
Isolamento: 9V Lego e 5V Arduino nunca se conectam. Todos GNDs unidos + GND do L293D.
Pinout Arduino → L293D
- Motor: pino 9 (EN1,2 PWM), pinos 2 e 5 (1A, 2A direção)
- Servo: pino 3 (EN3,4 PWM), pinos 6 e 8 (3A, 4A direção)
Lógica H-Bridge
| EN | 1A/3A | 2A/4A | Resultado |
|---|---|---|---|
| H | H | L | Frente / Esquerda |
| H | L | H | Ré / Direita |
Rotina de Controle
void SetMotorControl() {
// Motor
if (motorDirection == 1) { digitalWrite(2, HIGH); digitalWrite(5, LOW); }
else { digitalWrite(2, LOW); digitalWrite(5, HIGH); }
analogWrite(9, motorSpeed); // 0-255
// Servo
if (steeringDirection == 0) { digitalWrite(6, HIGH); digitalWrite(8, LOW); }
else { digitalWrite(6, LOW); digitalWrite(8, HIGH); }
analogWrite(3, steering); // 0-255
}
App Android com Joystick (MIT App Inventor 2)
App disponível na Play Store: "Arduino RC car bluetooth".
Designer: pad laranja + joystick azul. Blocks: rastreia toque (currentX, currentY) → calcula steering e speed (-100 a +100). Envia string via Bluetooth:
RC,steering,speed\n
Ex.: RC,-50,80\n (virar esquerda meia volta, frente 80%).
Leitura Bluetooth no Arduino
Conexão HC-05
- VCC → 5V Arduino (tolera 6V)
- GND → GND
- TXD (3,3V) → RX Arduino (pino 0) – direto
- RXD → TX Arduino (pino 1) – divisor de tensão 3× 1kΩ para 3,3V
Desconecte RX/TX do HC-05 ao fazer upload via USB.
Emparelhe: Bluetooth ON → app → "Bluetooth connect" → procure HC-05 → código 1234.
Código de Recepção
#define INPUT_SIZE 30
void loop() {
char input[INPUT_SIZE + 1];
byte size = Serial.readBytesUntil('\n', input, INPUT_SIZE);
input[size] = 0;
char* command = strtok(input, ","); // "RC"
char* RCsteering = strtok(NULL, ",");
char* RCspeed = strtok(NULL, ",");
int iRCsteering = atoi(RCsteering); // -100..100
int iRCspeed = atoi(RCspeed); // -100..100
// Converte para 0..255
steeringDirection = (iRCsteering > 0) ? 1 : 0;
steering = int(2.55 * abs(iRCsteering));
motorDirection = (iRCspeed > 0) ? 1 : 0;
motorSpeed = int(2.55 * abs(iRCspeed));
SetMotorControl();
}
Próximos Passos
- Adicione sensores ultrassônicos → robô autônomo (desvio de obstáculos)
- Detecte perda de Bluetooth e pare o modelo
- Migre para Adafruit Motor Shield (terminais de parafuso) + Sensor Shield
Compartilhe seus projetos!
Código Completo
/* Lego Power Functions via Bluetooth
Arduino Uno + L293D (motores pinos 2,5,9; servo pinos 6,8,3)
App Android MIT App Inventor 2 */
#include
#include
#define INPUT_SIZE 30
const int controlPin1A = 2, controlPin2A = 5, ENablePin = 9;
const int controlPin3A = 6, controlPin4A = 8, servoENablePin = 3;
int motorSpeed = 0, motorDirection = 1;
int steering = 0, steeringDirection = 0;
void setup() {
Serial.begin(9600); Serial.setTimeout(1000);
pinMode(controlPin1A, OUTPUT); pinMode(controlPin2A, OUTPUT); pinMode(ENablePin, OUTPUT);
pinMode(controlPin3A, OUTPUT); pinMode(controlPin4A, OUTPUT); pinMode(servoENablePin, OUTPUT);
digitalWrite(ENablePin, LOW); digitalWrite(servoENablePin, LOW);
}
void loop() {
char input[INPUT_SIZE + 1];
byte size = Serial.readBytesUntil('\n', input, INPUT_SIZE);
input[size] = 0;
char* command = strtok(input, ",");
char* RCsteering = strtok(NULL, ",");
char* RCspeed = strtok(NULL, ",");
int iRCsteering = atoi(RCsteering);
int iRCspeed = atoi(RCspeed);
if (iRCsteering > 0) steeringDirection = 1; else steeringDirection = 0;
steering = int(2.55 * abs(iRCsteering));
if (iRCspeed > 0) motorDirection = 1; else motorDirection = 0;
motorSpeed = int(2.55 * abs(iRCspeed));
SetMotorControl();
}
void SetMotorControl() {
if (motorDirection == 1) { digitalWrite(controlPin1A, HIGH); digitalWrite(controlPin2A, LOW); }
else { digitalWrite(controlPin1A, LOW); digitalWrite(controlPin2A, HIGH); }
analogWrite(ENablePin, motorSpeed);
if (steeringDirection == 0) { digitalWrite(controlPin3A, HIGH); digitalWrite(controlPin4A, LOW); }
else { digitalWrite(controlPin3A, LOW); digitalWrite(controlPin4A, HIGH); }
analogWrite(servoENablePin, steering);
}
Repositório GitHub (código Arduino, Fritzing, app MIT): github.com/Notthemarsian/Lego-car-Arduino
Esquemático
Disponível no GitHub acima.
Processo de manufatura
- Dominando o SaaS: Estratégias para Manter o Controle Enquanto Aproveita a Nuvem
- Derivação de Funções Exponenciais de Base e
- Encontrando Antiderivadas de Funções Exponenciais de Potência
- Aumente a Confiabilidade de Componentes de Transmissão de Potência: Estratégias Comprovadas para Mineração, Manufatura e Operações Industriais
- Painéis de Controle Elétricos: Fundamentos de Design para Segurança e ROI
- Energia Eólica: Convertendo o Movimento do Ar em Eletricidade Limpa
- Fabricantes do Reino Unido Perdem £2,5M Corrigindo Erros de Projeto em Projetos de Automação
- Aluguel Confiável de Energia e HVAC para Controle Climático Contínuo
- Oito Passos Comprovados para Diagnosticar e Corrigir Seu Sistema de Controle de Processo
- Dominando Controle de Tensão em Manuseio de Bobinas: Componentes Essenciais Explicados