WREN 12S ME SDK – Board Bring Up and Test Code
The WREN 12S ME SDK example code below is used for Board Bring Up and Test Input/Output Ports.
This Arduino IDE example (Settings. Board: ESP8266 12S Dev Module), basically exercises the RGB LED, Temperature Sensor, Voltage Sensor, Inputs and DC Motor Outputs.
Example UART Output
DitroniX WREN 12S SDK 1.1 Initialized Scanning I2C Found Devices: (0x18) (0x20) Found 2 Device(s). Connected to Digital Temperature Sensor MCP9808 Test PCB Temp: 28.0625°C Connected to I2C IO Expander MCP23017 Test RGB LEDs White Blue Green Red Motor 1 Cycle Check Motor 2 Cycle Check PCB Temp: 28.3125°C ADC Raw Value: 1024 DC Input Voltage: 30 V Waiting for Controller Input WREN Motor Driver 1A High PCB Temp: 28.5000°C ADC Raw Value: 1024 DC Input Voltage: 30 V WREN Motor Driver 1B High PCB Temp: 28.5000°C ADC Raw Value: 1024 DC Input Voltage: 30 V
Requires: Adafruit_MCP9808.h and Adafruit_MCP23017.h
/* Dave Williams, DitroniX 2020 WREN 12S SDK 1.0 DC Linear Motor Controller ESP12S (PCA v1.1 Nov 2020) Simplified Board Bring Up and Test Input/Output Ports The purpose of this code is tp cycle through the various functions This code is OPEN SOURCE and may freely be used, or modified as needed. No Wifi Enabled as not needed for bring up. */ /* * References to Supporting Libraries (Add to Managed Libraries) * MCP9808 Precision I2C Temperature Sensor - Adafruit_MCP9808 * https://learn.adafruit.com/adafruit-mcp9808-precision-i2c-temperature-sensor-guide * MCP23017 I2c Port Expander - Adafruit_MCP23017 * https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library */ /* Test LED Sequence * White - Check Temp Sensor or I2C * RGB Blue, Green and Red Self Test * Blue Long - Motor 1 Cycle Test * Green Long - Motor 2 Cycle Test * Red Heart Beat - Checking for Controller Input * Blue - Input Detected and Motor On */ #include <Arduino.h> #include "time.h" #include "Adafruit_MCP9808.h" #include "Adafruit_MCP23017.h" #include <Wire.h> // Create Adafruit objects Adafruit_MCP9808 TempSensor = Adafruit_MCP9808(); Adafruit_MCP23017 I2C_IO; // Configure WREN GPIO // **************** INPUTS **************** // Define I2C_IO Input Ports (from External 24V Inputs) #define WREN_IN_1A 8 //(Button 1 - DIN Pin 4 - Black) #define WREN_IN_2A 10 //(Button 2 - DIN Pin 2 - Yellow) #define WREN_IN_1B 9 //(Button 3 - DIN Pin 5 - Brown) #define WREN_IN_2B 11 //(Button 4 - DIN Pin 3 - Red) // Define ADC (12 DC - 30V DC) #define WREN_ADC A0 // Define I2C_IO Input Port Temp Alarm #define WREN_TALM 12 // **************** OUTPUTS **************** // Define I2C_IO Output Ports A (L6205 Inputs) #define WREN_EN_A 0 // Port A #define WREN_OUT_1A 1 //(DIN Pin 4 - Black) #define WREN_OUT_2A 2 //(DIN Pin 2 - Yellow) // Define I2C_IO Output Ports B (L6205 Inputs) #define WREN_EN_B 3 // Port B #define WREN_OUT_1B 4 //(DIN Pin 5 - Brown) #define WREN_OUT_2B 5 //(DIN Pin 3 - Red) // Define ESP Output Port LED (RGB LED) #define LED_Blue 16 #define LED_Green 12 #define LED_Red 2 // **************** IO **************** // Define I2C (Expansion Port) #define I2C_SDA 4 #define I2C_SCL 5 // Define Test Point IO #define TP_10 14 #define TP_11 15 // **************** VARIABLES **************** // Define DIN Input Ports (from External 24V/29V Inputs) int WREN_IN_1A_State = 0; // DIN 4 int WREN_IN_2A_State = 0; // DIN 2 int WREN_IN_1B_State = 0; // DIN 5 int WREN_IN_2B_State = 0; // DIN 3 int WREN_Bridged_State = 0; // Special Bridged Parallel Mode int WREN_Input_State = 0; // Special Bridged Parallel Mode int MCP_IO_Int = 13; int ButtonVal_1A = 0; // 1 int ButtonVal_2A = 0; // 2 int ButtonVal_1B = 0; // 3 int ButtonVal_2B = 0; // 4 float PCB_Temp; // **************** Default Addresses **************** int MCP9808 = 0x18; int MCP_IO = 0x20; // Setup void setup() { delay(250); //Settle // **************** COMMS **************** // Initialize UART: Serial.begin(115200, SERIAL_8N1); //115200 Serial.println(""); Serial.println("DitroniX WREN 12S SDK 1.1 Initialized"); Serial.println(""); // Initialize I2C Wire.begin(I2C_SDA, I2C_SCL); // I2C Scan Devices Serial.print("Scanning I2C\t"); byte count = 0; Serial.print("Found Devices: "); for (byte i = 8; i < 120; i++) { Wire.beginTransmission(i); if (Wire.endTransmission() == 0) { Serial.print(" (0x"); Serial.print(i, HEX); Serial.print(")\t"); count++; delay(1); } } Serial.print("Found "); Serial.print(count, HEX); Serial.println(" Device(s)."); // PCB Temperature Sensor if (!TempSensor.begin()) { // Sensor or I2C Issue Serial.println("Couldn't find MCP9808! Sensor or I2C Issue. Check the PCB !!"); digitalWrite(LED_Blue, LOW); digitalWrite(LED_Green, LOW); digitalWrite(LED_Red, LOW); delay(2000); } else { Serial.print("Connected to Digital Temperature Sensor MCP9808\t"); // Set Resolution 0.0625°C 250 ms and Wake Up TempSensor.setResolution(3); // Read PCB_Temp Sensor and Output TempSensor.wake(); // Wake Up PCB_Temp = TempSensor.readTempC(); Serial.print("Test PCB Temp: "); Serial.print(PCB_Temp, 4); Serial.println("°C"); TempSensor.shutdown_wake(1); // Sleep } // I2C IO Expander I2C_IO.begin(); Serial.println("Connected to I2C IO Expander MCP23017\t"); // **************** INPUTS **************** // Initialize Input Ports (from External 24V Inputs) I2C_IO.pinMode (WREN_IN_1A, INPUT); I2C_IO.pinMode (WREN_IN_2A, INPUT); I2C_IO.pinMode (WREN_IN_1B, INPUT); I2C_IO.pinMode (WREN_IN_2B, INPUT); // Initialize Temp Alarm (unused in this test code) I2C_IO.pinMode (WREN_TALM, INPUT); // Initialize MCP INT (unused in this test code) I2C_IO.pinMode(MCP_IO_Int, INPUT_PULLUP); // **************** OUTPUTS **************** // Initialize Output Ports A (L6205 Inputs) I2C_IO.pinMode (WREN_EN_A, OUTPUT); I2C_IO.pinMode (WREN_OUT_1A, OUTPUT); I2C_IO.pinMode (WREN_OUT_2A, OUTPUT); // Initialize Output Ports B (L6205 Inputs) I2C_IO.pinMode (WREN_EN_B, OUTPUT); I2C_IO.pinMode (WREN_OUT_1B, OUTPUT); I2C_IO.pinMode (WREN_OUT_2B, OUTPUT); // Initialize LED (RGB LED) pinMode (LED_Blue, OUTPUT); pinMode (LED_Green, OUTPUT); pinMode (LED_Red, OUTPUT); // **************** I/O **************** // Test Cycle RGB LED Serial.println("Test RGB LEDs White Blue Green Red"); digitalWrite(LED_Blue, LOW); digitalWrite(LED_Green, LOW); digitalWrite(LED_Red, LOW); delay(1000); digitalWrite(LED_Blue, HIGH); digitalWrite(LED_Green, HIGH); digitalWrite(LED_Red, HIGH); delay(250); digitalWrite(LED_Blue, LOW); delay(1000); digitalWrite(LED_Blue, HIGH); delay(250); digitalWrite(LED_Green, LOW); delay(1000); digitalWrite(LED_Green, HIGH); delay(250); digitalWrite(LED_Red, LOW); delay(1000); digitalWrite(LED_Red, HIGH); delay(250); // Enable both Motor Driver Ports I2C_IO.digitalWrite(WREN_EN_A, HIGH); I2C_IO.digitalWrite(WREN_EN_B, HIGH); // Test Cycle Motor 1 (A) Serial.println("Motor 1 Cycle Check"); digitalWrite(LED_Blue, LOW); I2C_IO.digitalWrite(WREN_OUT_1A, HIGH); I2C_IO.digitalWrite(WREN_OUT_2A, LOW); delay(2000); I2C_IO.digitalWrite(WREN_OUT_2A, HIGH); I2C_IO.digitalWrite(WREN_OUT_1A, LOW); delay(2000); I2C_IO.digitalWrite(WREN_OUT_1A, LOW); I2C_IO.digitalWrite(WREN_OUT_2A, LOW); digitalWrite(LED_Blue, HIGH); // Test Cycle Motor 2 (B) Serial.println("Motor 2 Cycle Check"); digitalWrite(LED_Green, LOW); I2C_IO.digitalWrite(WREN_OUT_1B, HIGH); I2C_IO.digitalWrite(WREN_OUT_2B, LOW); delay(2000); I2C_IO.digitalWrite(WREN_OUT_2B, HIGH); I2C_IO.digitalWrite(WREN_OUT_1B, LOW); delay(2000); I2C_IO.digitalWrite(WREN_OUT_1B, LOW); I2C_IO.digitalWrite(WREN_OUT_2B, LOW); digitalWrite(LED_Green, HIGH); CheckSensors(); Serial.println("Waiting for Controller Input"); Serial.println(""); } // Sensors void CheckSensors() { // Read PCB_Temp Sensor and Output TempSensor.wake(); // Wake Up PCB_Temp = TempSensor.readTempC(); Serial.print("PCB Temp: "); Serial.print(PCB_Temp, 4); Serial.print("°C\t"); TempSensor.shutdown_wake(1); // Sleep // Read ADC DC Voltage Sensor and Output Serial.print("ADC Raw Value: "); Serial.print(analogRead(WREN_ADC)); Serial.print("\t DC Input Voltage: "); Serial.print(((analogRead(WREN_ADC) * 30 ) / 1024)); Serial.println(" V"); Serial.println(""); } // Loop void loop() { //Read Input / Buttons ButtonVal_1A = I2C_IO.digitalRead(WREN_IN_1A); // Read the input pin 1A ButtonVal_2A = I2C_IO.digitalRead(WREN_IN_2A); // Read the input pin 2A ButtonVal_1B = I2C_IO.digitalRead(WREN_IN_1B); // Read the input pin 1B ButtonVal_2B = I2C_IO.digitalRead(WREN_IN_2B); // Read the input pin 2B // Basic Detection. If any Input High then set appropriate Motor Output High if (ButtonVal_1A == HIGH || ButtonVal_1B == HIGH || ButtonVal_2A == HIGH || ButtonVal_2B == HIGH) { if (ButtonVal_1A == HIGH) { I2C_IO.digitalWrite(WREN_OUT_1A, HIGH); // Sets Motor Driver Output High if Input High Serial.println("WREN Motor Driver 1A High"); } if (ButtonVal_2A == HIGH) { I2C_IO.digitalWrite(WREN_OUT_2A, HIGH); // Sets Motor Driver Output High if Input High Serial.println("WREN Motor Driver 2A High"); } if (ButtonVal_1B == HIGH) { I2C_IO.digitalWrite(WREN_OUT_1B, HIGH); // Sets Motor Driver Output High if Input High Serial.println("WREN Motor Driver 1B High"); } if (ButtonVal_2B == HIGH) { I2C_IO.digitalWrite(WREN_OUT_2B, HIGH); // Sets Motor Driver Output High if Input High Serial.println("WREN Motor Driver 2B High"); } // Heat Beat Blue LED digitalWrite(LED_Blue, LOW); delay(10); digitalWrite(LED_Blue, HIGH); CheckSensors(); } else { // Sets All Motor Driver Outputs Low I2C_IO.digitalWrite(WREN_OUT_1A, LOW); I2C_IO.digitalWrite(WREN_OUT_2A, LOW); I2C_IO.digitalWrite(WREN_OUT_1B, LOW); I2C_IO.digitalWrite(WREN_OUT_2B, LOW); // Heat Beat Red LED delay(250); digitalWrite(LED_Red, LOW); delay(10); digitalWrite(LED_Red, HIGH); } }