Fabrication industrielle
Internet des objets industriel | Matériaux industriels | Entretien et réparation d'équipement | Programmation industrielle |
home  MfgRobots >> Fabrication industrielle >  >> Manufacturing Technology >> Processus de fabrication

Maîtrisez Lego Power Functions avec Arduino et contrôle Bluetooth

Composants et fournitures

Composant Quantité
Arduino UNO 1
Double pont en H L293D (Texas Instruments) 1
Breadboard 1
Connecteur 9V vers jack barrel 1
Rallonge Lego Power Functions (réf. 8886) 3
Module Bluetooth HC-05 1

À propos du projet

Vous avez un modèle Lego Power Functions avec moteurs et servos ? Voici comment le piloter avec un Arduino et un minimum de composants. Explications détaillées pour débutants. Le modèle Lego, vous le construisez (ou votre fils).

Nous créerons aussi une télécommande Android avec MIT App Inventor. Bientôt votre rover Lego Mars dans le jardin !

Mon fils a un Lego Technic 9398 (4X4 Crawler) : 1 servo direction + 2 moteurs L pour la traction. Test du prototype :

Comprendre le câblage Power Functions

Achetez des rallonges Lego Power Functions (8886) et coupez-les. Nous ferons des câbles « breakout » : brique Lego d'un côté, broches de l'autre.

Codage des fils (vue de face, détrompeur en haut) :

Pour le servo : soudez des broches sur les 4 fils, utilisez la fiche grise claire.
Pour l'alimentation (boîte à piles → breadboard) : seulement +9V et GND, fiche grise foncée (mâle requis).
Pour les moteurs : seulement C1 et C2, fiche grise claire.

Contrôler un moteur Lego avec le L293D

Vitesse variable (PWM) pour moteurs DC, position pour servo. Arduino : analogWrite(pin, 0-255) sur broches PWM (marquées ~).

L'Arduino sort du 5V/30mA max. Les moteurs Lego veulent 9V/>100mA. Il faut un « dispositif de commutation » : le pont en H. Le L293D en contient deux sur une puce → 1 côté pour les moteurs (en parallèle), 1 côté pour le servo. (Pour contrôler les 2 moteurs indépendamment, il faut un 2e L293D).

Règle d'or : Jamais relier le +9V Lego au +5V Arduino ! Mais toutes les masses (GND) doivent être communes avec les broches GND du L293D.

Broches Arduino utilisées

Logique du L293D

EN1,21A2ARésultat
HighHighLowMoteur avant (motorDirection=1)
HighLowHighMoteur arrière (motorDirection=0)
EN3,43A4ARésultat
HighHighLowServo gauche (steeringDirection=0)
HighLowHighServo droite (steeringDirection=1)

Sous-programme de contrôle


void SetMotorControl() {
  // Moteurs
  if (motorDirection == 1) { digitalWrite(controlPin1A, HIGH); digitalWrite(controlPin2A, LOW); }
  else { digitalWrite(controlPin1A, LOW); digitalWrite(controlPin2A, HIGH); }
  analogWrite(ENablePin, motorSpeed);

  // Servo
  if (steeringDirection == 0) { digitalWrite(controlPin3A, HIGH); digitalWrite(controlPin4A, LOW); }
  else { digitalWrite(controlPin3A, LOW); digitalWrite(controlPin4A, HIGH); }
  analogWrite(servoENablePin, steering);
}

Application Android simple avec joystick (MIT App Inventor 2)

Testez l'appli finie : Arduino RC car bluetooth sur Play Store.

MIT App Inventor 2 : introduction innovante à la programmation d'appli (Google/MIT). Installez MIT AI2 Companion sur votre Android. Sur PC, ouvrez le lien du projet App Inventor (Firefox/Chrome), connectez-vous avec Gmail.

L'interface : un pad orange avec boule bleue (joystick). En mode Blocs : un bloc when TouchScreen.Dragged suit le doigt.

Variables currentX, currentY → calculent steering et speed (-100 à +100). Limitation si doigt hors pad.

Commande envoyée par Bluetooth (format chaîne) :


RC,steering,speed,\n

\n signale la fin de commande à l'Arduino.

Lecture des commandes Bluetooth sur Arduino

Module HC-05 : 6 broches, on utilise VCC, GND, TXD, RXD. VCC → 5V Arduino (tolère 6V). TXD/RXD : croiser (TXD module → RXD Arduino pin 0, RXD module → TXD Arduino pin 1).

Attention : Module en logique 3,3V. Le RXD du module ne supporte pas 5V direct. Il faut un diviseur de tension (3 × 1 kΩ) pour abaisser le 5V Arduino à ~3,3V vers le module. Le TXD module (3,3V) peut aller direct sur RXD Arduino (reconnu comme HIGH).

Pins 0/1 partagés avec l'USB : débranchez RXD/TXD du module pendant l'upload USB.

Appairage : Bluetooth ON → app → « Bluetooth connect » → visible → cherchez « HC-05 » → code « 1234 » → « connected » en vert.

Code de lecture


#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, ",");    // -100..100
  char* RCspeed = strtok(NULL, ",");       // -100..100

  int iRCsteering = atoi(RCsteering);
  int iRCspeed = atoi(RCspeed);

  // Conversion -100..100 → 0..255 et sens
  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();
}

Et après ?

Partagez vos projets !

Code complet


/* Lego Power Functions piloté par Bluetooth
   Arduino Uno contrôle moteurs + servo
   Télécommande Android (MIT App Inventor 2)
   Circuit :
   - Série (pins 0,1) : module Bluetooth (débrancher pour upload USB)
   - L293D moteurs (pins 2,5,9)
   - L293D servo  (pins 6,8,3)
*/
#include 
#include 

#define INPUT_SIZE 30

const int controlPin1A = 2;
const int controlPin2A = 5;
const int ENablePin = 9;

const int controlPin3A = 6;
const int controlPin4A = 8;
const int 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);
}

Sources GitHub (code Arduino, schéma Fritzing, source App Inventor) : github.com/Notthemarsian/Lego-car-Arduino

Schéma

Disponible sur le GitHub ci-dessus : Lego rc car v2.fzz (Fritzing)

Processus de fabrication

  1. Maîtriser le SaaS : stratégies pour garder le contrôle tout en exploitant le cloud
  2. Primitives des fonctions exponentielles de base e
  3. Booster la fiabilité des composants de transmission de puissance : stratégies éprouvées pour mines, fabrication et industrie
  4. Panneaux de contrôle électriques : Essentiels de conception pour la sécurité et le ROI
  5. L'énergie éolienne : transformer le mouvement de l'air en électricité propre
  6. Les fabricants britanniques perdent 2,5 M£ à corriger des erreurs de conception dans les projets d'automatisation
  7. Locations fiables d'énergie et CVC pour un contrôle climatique sans faille
  8. Huit étapes éprouvées pour diagnostiquer et corriger votre système de contrôle de process
  9. Maîtriser le contrôle de tension dans le traitement de bande : composants essentiels expliqués