ESP8266 : sorties

Attention, pour la Wemos D1 R1 development board, des ambiguïtés subsistent concernant les sorties D8 (GPIO0) et D9 (GPIO2) qui sont en pull-up et la sortie D10 (GPIO15) en pull-down.

Les sorties sont en 10 bits (0-1023)

Clignoter une LED

Rien d'original si ce n'est que la LED intégrée au module est sur la sortie n°2. À savoir, la variable “BUILTIN_LED” permet de s'affranchir du numéro physique de la sortie.

int led = 2;
 
void setup() {                
  pinMode(led, OUTPUT);     
}
 
void loop() {
  digitalWrite(led, HIGH);
  delay(1000);
  digitalWrite(led, LOW);
  delay(1000);
}

Plusieurs LEDs

Wemos D1

Sur la carte Wemos D1, les numéros de sorties sont un peu différents. Par exemple ici pour les sorties 13, 12 et 11 d'une Arduino classique, il s'agit en fait de 14, 12 et 13 (ou on peut aussi écrire D13, D12, D11).

const int led1 = 14; //D13
const int led2 = 12; //D12
const int led3 = 13; //D11
 
//PWM : 10 bits
 
int brightness = 0;        // how bright the LED is (0 = full, 512 = dim, 1023 = off)
int fadeAmount = 5;        // how many points to fade the LED by
const int delayMillis = 10;// how long to pause between each loop
 
void setup() {
  pinMode(led1, OUTPUT); 
  pinMode(led2, OUTPUT); 
  pinMode(led3, OUTPUT); 
}
 
void loop() {
  // set the LED brightness
  analogWrite(led1, brightness);
  analogWrite(led2, brightness);
  analogWrite(led3, brightness);
 
  // increment/decrement the brightness for the next loop
  brightness = brightness + fadeAmount;
 
  // limit to 10-bit (0-1023)
  if (brightness < 0) brightness = 0;
  if (brightness > 1023) brightness = 1023;
 
  // reverse the direction of the fading at each end
  if (brightness == 0 || brightness == 1023) {
    fadeAmount = -fadeAmount;
  }
 
  // pause so you can see each brightness level
  delay(delayMillis);
}

Wemos mini

/* Wemos Mini : test three outputs D5, D6, D7
 *  
 *  Mapping Wemos     Arduino
 *  D0                16    Digital (HIGH = on, LOW = OFF)
 *  D5                14*   Digital (HIGH = on, LOW = OFF), Analog (1023 = HIGH, 0 = LOW)
 *  D6                12*   Digital (HIGH = on, LOW = OFF), Analog (1023 = HIGH, 0 = LOW)
 *  D7                13*   Digital (HIGH = on, LOW = OFF), Analog (1023 = HIGH, 0 = LOW)
 *  D8                15*   Digital (HIGH = on, LOW = OFF), Analog (1023 = HIGH, 0 = LOW)
 *  D1                5*   Digital (HIGH = on, LOW = OFF), Analog (1023 = HIGH, 0 = LOW)
 *  D2                4*   Digital (HIGH = on, LOW = OFF), Analog (1023 = HIGH, 0 = LOW)
 *  D4                2*   Digital (HIGH = on, LOW = OFF), Analog (1023 = HIGH, 0 = LOW)
 *  D3 ??
 *  D0 ??
 *  D1 ??
 */
 
#define LED1 14 // ou D5
#define LED2 12 // ou D6
#define LED3 13 // ou D7
 
void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
}
 
void loop() {
  digitalWrite(LED1, HIGH);
  delay(5000);
  digitalWrite(LED1, LOW);
  delay(2000);
 
  analogWrite(LED2, 1023);
  delay(5000);
  analogWrite(LED2, 0);
  delay(2000);
 
  for (int i = 0 ; i < 1023 ; i=i+10){
    analogWrite(LED1, i);
    analogWrite(LED2, i);
    analogWrite(LED3, i);
    delay(20);
  }
 
  for (int i = 0 ; i < 1023 ; i=i+10){
    analogWrite(LED1, i);
    delay(20);
  }
 
  for (int i = 0 ; i < 1023 ; i=i+10){
    analogWrite(LED2, i);
    delay(20);
  }
 
  for (int i = 0 ; i < 1023 ; i=i+10){
    analogWrite(LED3, i);
    delay(20);
  }
}

Servo motors

Wemos mini

/*
 * Wemos mini > 2 servos
 * 
 * Wemos mapping
 * D1				Servo 1 signal
 * D2				Servo 2 signal
 * GND				GND
 * 					5V (extern)
 */
 
#include <Servo.h> 
 
Servo servo1, servo2;
 
void setup() 
{ 
 
} 
 
void loop() 
{ 
  int pos;
 
  // Attach servos
  servo1.attach(D1);
  servo2.attach(D2);
 
  for(pos = 0; pos <= 180; pos += 1)
  {                                 
    servo1.write(pos);
    servo2.write(180-pos);
    delay(15);                       
  } 
 
  for(pos = 180; pos>=0; pos-=1)      
  {                                
    servo1.write(pos);
    servo2.write(180-pos);
    delay(15);                     
  } 
 
  delay(3000);
 
 // Detach servos (needed sometimes sounds from servo are off)
  servo1.detach();
  servo2.detach();
 
  delay(3000);
} 

Monster shield

Wemos mini

/*  Wemos Mini > Monster Shield
 *    
 *  Mapping Wemos     MonsterShield
 *  D0                D7 : CW digital (pins 7 et 9 soudées : inversées) 
 *  D5                D8 : CCW digital (pins 8 et 4 soudées : inversées) 
 *  D6                D5 : PWM1
 *  D7                D6 : PWM2
 *  5V                5V
 *  GND               GND
 *                    9V
 */
 
#define CW 16
#define CCW 14
#define PWM1 12
#define PWM2 13
 
void setup() {
  pinMode(PWM1, OUTPUT);
  pinMode(PWM2, OUTPUT);
  pinMode(CW, OUTPUT);
  pinMode(CCW, OUTPUT);
}
 
void loop() {
  // direction CW
  digitalWrite(CW, HIGH);
  digitalWrite(CCW, LOW);
 
  analogWrite(PWM1, 0);
  analogWrite(PWM2, 0);
  delay(3000);
 
  analogWrite(PWM1, 400); // ! PWM / 1023
  analogWrite(PWM2, 400);
  delay(3000);
 
  // direction CCW
  digitalWrite(CW, LOW);
  digitalWrite(CCW, HIGH);
 
  analogWrite(PWM1, 0);
  analogWrite(PWM2, 0);
  delay(3000);
 
  analogWrite(PWM1, 800);
  analogWrite(PWM2, 800);
  delay(3000);
}