esp8266 uploading/compiling error 'D1' was not declared in this scope

hey i am having some trouble uploading some code to my esp8266 when i try to compile or upload i get the message “exit status 1, ‘D1’ was not declared in this scope”

this is the code i try to upload

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "***";
const char* password = "***";

#define MOTOR_PWM_PIN D1
#define MOTOR_DIR_PIN D3

#define TRAIN_BRAKE 4

#define I_AM_A_TRAIN 1
#define I_AM_A_LEFT_SWITCH 2
#define I_AM_A_SEMAPHORE 4

WiFiUDP Udp;
unsigned int localUdpPort = 4711;  // local port to listen on
char incomingPacket[255];  // buffer for incoming packets
char outgoingPacket[255];
char replyA[] = "At hall A";
char replyB[] = "At hall B";
char reply[] = "Hello there!";

int currentSpeed = 0;
int currentDirection = 0;
const int minSpeedEquivalent = 512;
const int maxSpeedEquivalent = 800;
const int maxSpeed = 200;

bool onMagnet = false;
unsigned long onMagnetTime = 0;
int deltaCount = 0;
IPAddress remoteIP = IPAddress(0,0,0,0);
uint16_t remotePort = 0;

void setup() {
 pinMode(LED_PIN, OUTPUT);
 attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_A_PIN), onHallA, FALLING);
 attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_B_PIN), onHallB, FALLING);

 digitalWrite(LED_PIN, HIGH);
 Serial.printf("Connecting to %s ", ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 Serial.println(" connected");

 Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
 digitalWrite(LED_PIN, LOW);

void loop()
 int packetSize = Udp.parsePacket();
 if (packetSize)
   remoteIP = Udp.remoteIP();
   remotePort = Udp.remotePort();
   // receive incoming UDP packets
   Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
   int len =, 255);
   if (len > 1)
     if (incomingPacket[1] == TRAIN_CONTROL) {
       int speed = readInt(incomingPacket, 2);
       Serial.printf("Speed: %d\n", speed);
     else if (incomingPacket[1] == TRAIN_BRAKE) {
       sendTrainInformation(0, 0, 0, 1);
     else {
       incomingPacket[len] = 0;
       Serial.printf("UDP packet contents: %s\n", incomingPacket);


   // send back a reply, to the IP address and port we got the packet from

void broadcastExistence() {

int readInt(char buffer[], int index) {
 int result = 0;
 for (int i = index + 3; i >= index; i--) {
   result <<= 8;
   result |= buffer[i];
 return result;

int writeInt(char buffer[], int index, int value) {
 for (int i = index; i < index + 4; i++) {
   buffer[i] = value & 255;
   value >>= 8;
 return index + 4;

void applySpeed(int speed) {
 int d = (maxSpeedEquivalent - minSpeedEquivalent)/maxSpeed;
 int speedEquivalent = minSpeedEquivalent + speed*d;
 if (speed == 0) speedEquivalent = 0;
 digitalWrite(MOTOR_DIR_PIN, currentDirection);
 analogWrite(MOTOR_PWM_PIN, speedEquivalent);

void onHallA() {
 digitalWrite(LED_PIN, HIGH);
 if (onMagnet) return;
 onMagnet = true;
 onMagnetTime = millis();

void onHallB() {
 digitalWrite(LED_PIN, LOW);
 if (remotePort == 0 || !onMagnet) return;
 onMagnet = false;
 onMagnetTime = millis() - onMagnetTime;
 sendTrainInformation((5000/onMagnetTime), 0, 0, 1);

void sendTrainInformation(int speed, int trackId, int distanceToLight, int light) {
 outgoingPacket[0] = 15;
 outgoingPacket[1] = TRAIN_INFORMATION;
 writeInt(outgoingPacket, 2, speed);
 writeInt(outgoingPacket, 6, trackId);
 writeInt(outgoingPacket, 10, distanceToLight);
 outgoingPacket[14] = light;
 Udp.beginPacket(remoteIP, remotePort);
 Udp.write(outgoingPacket, 15);

if i try and add ’ ’ to the #define’s it get compiled but i get an error in the serial monitor saying

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d

Please go back and edit your post to put in code tags so your code isn't half in italics. You'd know about that if you'd read "How to use this forum - please read" at the top of every forum.

Then post the COMPLETE error message(s) from the compiler not your mangled version. E.g. your subject says 'd1' was not declared. If that's really what the error says then it's correct, there is no 'd1' declared in your program. There is a D1 but that's different.


Are you sure that was the error? I don't see any use of d1 in your code. I do see D1 though. The D1, etc. pin numbers are only defined for certain ESP8266 boards like the WeMos D1 Mini. If the board you're using is not one of these, then you should replace D1, etc, with the correct pin numbers on your board.

The whole Dn pin numbering thing is a bit of a hack. The ESP8266 has GPIO numbers and those numbers are used by the ESP8266 core. But some board manufacturers wanted to number their pins differently, either to make them line up in consecutive order after routing, or to emulate the Uno's pinout (WeMos D1 and D1 R2). So they put pin numbers on the silkscreen of their boards that didn't match the ESP8266 GPIO numbers. In order to accommodate this, the ESP8266 core defined some variables to map the GPIO numbers to the numbers on the boards. Here it is for the "LOLIN(WEMOS) D1 R2 & mini" board:

static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;

thanks for the feedback i have edited my post now.

it sure is D1 not d1. i look for the manufactor it says AI thinker esp8266mod and there is a motor shield connected to it.sorry for the lack of info, i got the hardware and code from my school.