Arduino_tutorial


Note

The following will override the ESP32 factory default code!

Tools:

  1. PC(Win10 or uper)

  2. Type C USB cable

Install the CH340 driver


Click me

Note

After the driver is installed, you must install the appropriate battery and turn the eCar power switch on, otherwise you will not find the COM port!

img

Tip

If you’ve already done this step, you can skip it!

Install the Arduino IDE


Click me

Tip

If you’ve already done this step, you can skip it!

Installing libraries


  1. Download the library file and unzip it.
    Click me to download!

Tip

The library files downloaded above are tested by us, they may not be the latest library files!

  1. Install the library files downloaded above into the Arduino IDE.
    Refer to: Link
    img

Note

It is possible that you have already installed these libraries, but the version of the library is different, and there will be an error message during the installation process. We recommend deleting the installed libraries and using the libraries we provide!

Latest library resources(option):
ESP32-audioI2S-master: https://github.com/schreibfaul1/ESP32-audioI2S
ESP32Servo: https://madhephaestus.github.io/ESP32Servo/annotated.html
IRremote: https://github.com/Arduino-IRremote/Arduino-IRremote

Configure the Arduino IDE for ESP32


  1. Open the arduino IDE,click ā€œFileā€ > ā€œPreferencesā€ļ¼Œas shown below:
    img

  2. Open the button marked below:
    img

  3. Copy the link:

https://espressif.github.io/arduino-esp32/package_esp32_index.json
  1. Paste it inside and click OK, as shown below:
    img

  2. Click ā€œBoards Managerā€:
    img

  3. Find the ESP32 from the pop up Boards Manager and then click install.
    img

Tip

It is recommended to install version 2.0.14, which we are using!

  1. Click ā€œToolsā€ > ā€œBoardā€ > ā€œesp32ā€ to choose the ā€œESP32 Dev Moduleā€.
    img

  2. ESP32 parameter Settings.
    img

Note

All the following project code needs to be set according to the above parameters, or you will get an error!

Project


Download the example code:
Click me to download!

1_Touch

  1. Open the ā€œtouchā€ example code:
    img

  2. Select board type:
    img

  3. Select COM port:
    img

Note

The PC must have the CH340 driver installed, the eCar has the proper battery installed, and the power switch is turned on, otherwise the COM port will not be found!

  1. ESP32 parameter Settings:
    img

  2. Upload the code:
    img

  3. Open the serial monitor and select the baud rate:
    img

  4. Result:
    img
    Click the touch pad of eCar, and the serial port monitor displays the data.
    img

Code analysis:

  1. Initialize the serial port baud rate to 115200:

Serial.begin(115200);
  1. After printing serial port data, no line breaks:

Serial.print("T2(IO2) = ");    
  1. After printing touch data on the serial port, newline:

Serial.println(touchRead(T2));     
  1. Initialize the touch interrupt:

// For tuch T2(IO02).
touchAttachInterrupt(T2, gotTouchT2, threshold);
  1. Define the touch interrupt mode:

// Touch ISR will be activated when touchRead is lower than the Threshold
touchInterruptSetThresholdDirection(testingLower);
  1. Touch interrupt function:

// Interrupt function for touch T2(IO02).
void gotTouchT2(){
  if (lastTouchActive != testingLower) {
    touchActive = !touchActive;
    testingLower = !testingLower;
    // Touch ISR will be inverted: Lower <--> Higher than the Threshold after ISR event is noticed
    touchInterruptSetThresholdDirection(testingLower);
  }
}

2_Ultrasonic_module

  1. Open the ā€œultrasonic_moduleā€ example code and upload it to eCar:
    img

  2. Result:
    The serial port monitor prints the distance measured by the ultrasonic module.
    img
    img

Code analysis:

  1. Define a thread.

//Define the first thread 
xTaskCreate(
TASK_ONE,          // Task function
"TaskOne",         // Task name
32*1024,           // Stack size, set as needed
NULL,              
1,                 // priority
&TASK_HandleOne    // Task handle
);
  1. Thread function, which runs in parallel with loop():

// The function body of task1, since the input parameter is NULL, 
// so the function body needs to be void * parameter, otherwise an error is reported.
void TASK_ONE(void *param ){
  for(;;){
    ...
  }
  // When the thread terminates, the thread resource is released.
  //Serial.println("Ending TaskOne!");
  //vTaskDelete( TASK_HandleOne );
}
  1. Generate a 10 microsecond high level trigger signal to the ultrasonic module:

// trig 10us
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
  1. Read the high level time generated by the ECHO pin of the ultrasonic module, which is equal to the transmission time of the sound wave:

// Calculate the time of the ultrasonic echo
while(digitalRead(echoPin) == LOW){
    delayMicroseconds(10);
    if(T > 10000){ break; }  // About 100 milliseconds.
    T++;
}
T = 0;
while(digitalRead(echoPin) == HIGH){
    delayMicroseconds(10);
    if(T > 10000){ break; }  // About 100 milliseconds.
    T++;
}
  1. Calculate the distance of the object

long dis = T * (10 + readEchoPinErr) * 0.034 / 2;

More information about ultrasonic module: Link

3_Battery_voltage

  1. Open the ā€œvoltageā€ example code and upload it to eCar:
    img

  2. Result:
    The serial port monitor prints the digital analog value and voltage value of the battery.
    img

Code analysis:

  1. Set the resolution of the ADC, which can be configured with 12 bit, 11 bit, 10 bit and 9 bit resolutions.

analogReadResolution(12);
  1. Read the analog / millivolts value for pin34.

int analogValue = analogRead(34)*2;   
int analogVolts = analogReadMilliVolts(34)*2;  

Note: Because two 10K divider resistors are used to sample the voltage of the battery, it needs to be multiplied by two.
img

4_Servo

  1. Open the ā€œservoā€ example code and upload it to eCar:
    img

  2. Result:
    Servo cycle 0–180 degrees, 180–0 degrees swing.
    img

Code analysis:

  1. Include the servo drive library into the code.

#include <ESP32Servo.h>    
  1. Initialize the two servos:

// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myAservo.setPeriodHertz(50);  // Standard 50hz servo
myBservo.setPeriodHertz(50);  // Standard 50hz servo

// attaches the servo on pin to the servo object
// using SG90 servo min/max of 500us and 2400us
// for MG995 large servo, use 1000us and 2000us,
// which are the defaults, so this line could be
// "myservo.attach(servoPin);"
myAservo.attach(AservoPin, 500, 2400);   
myBservo.attach(BservoPin, 500, 2400);  
myAservo.write(90);
myBservo.write(90);
  1. Set the Angle of the two servos to 90 degrees:

myAservo.write(90);
myBservo.write(90);

More information about servo: Link

5_RGB_LED

A WS2812 RGB LED module is used on eCar to generate different colors of light.

  1. Open the ā€œRGB_LEDā€ example code and upload it to eCar:
    img

  2. Result:
    RGB LEDs first produce white light, and then cycle through to produce red, green, and blue light.
    img

Code analysis:

  1. An array that stores color values. The value range is: 0-255, corresponding to green, red, and blue respectively.

int color[] =  {0x55, 0x11, 0x77};  // RGB value: G, R, B
  1. The color value is converted to the WS2182 color value to be displayed and only one RGB LED is lit.

int i=0, led, col, bit;
for (led=0; led<NR_OF_LEDS; led++) {
  for (col=0; col<3; col++ ) {
    for (bit=0; bit<8; bit++){
      if ( (color[col] & (1<<(7-bit))) && (led == RGBled_index) ) {
        led_data[i].level0 = 1;
        led_data[i].duration0 = 8;
        led_data[i].level1 = 0;
        led_data[i].duration1 = 4;
      } else {
        led_data[i].level0 = 1;
        led_data[i].duration0 = 4;
        led_data[i].level1 = 0;
        led_data[i].duration1 = 8;
      }
      i++;
    }
  }
}
  1. Send data to WS2812.

rmtWrite(rmt_send, led_data, NR_OF_ALL_BITS);   

6_SD_card

  1. Insert the SD card into the eCar.
    img

  2. Open the ā€œSD_cardā€ example code and upload it to eCar:
    img

  3. Result:
    Print all the contents in the root directory of the SD card on the serial monitor.
    img

Code analysis:

  1. Initialize the SPI port.

pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
  1. Initialize the SD card.

if(!SD.begin(SD_CS)){
    Serial.println("Card Mount Failed");
    return;
}
  1. Call ā€œlistDirā€ function to print all the contents in the root directory of the SD card on the serial monitor.

listDir(SD, "/", 0);

More information about SPI communication protocol: Link

7_Speaker

  1. Open the ā€œspeakerā€ example code and upload it to eCar:
    img

  2. Result:
    The speaker produces 440 Hz sound all the time.

Tip

Make sure the speaker is plugged into eCar!

Code analysis:

  1. Initialize the I2S master.

if (!I2S.begin(mode, sampleRate, bps)) {
Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
  1. Send data to the I2S slave.

I2S.write(sample);    

8_MP3_player

  1. Insert the SD card into the eCar.
    img

  2. Open the ā€œmp3_playerā€ example code and upload it to eCar:
    img

  3. Result:
    eCar will always loop the songs from the SD card.

Code analysis:

  1. Define a 2-dimensional array to store the song names in the SD card.

char songs[][40]={
  "1_Free Loop.mp3",
  "2_Dream It Possible.mp3",
  "3_We Are The Brave.mp3",
  "4_My stupid heart.mp3",
  "5_She.mp3",
  "6_Big Big World.mp3",
  "7_My Love.mp3",
  "8_Hero.mp3",
  "9_Home.mp3",
  "10_I Got You.mp3",
  "11_Just One Last Dance.mp3",
  "12_Peaches.mp3",
  "13_Valder Fields.mp3",
  "14_You Raise Me Up.mp3"
};

Note

The song names in the 2-dimensional array must be the same as the song names in the SD card, otherwise the songs cannot be played!

  1. Initialize the I2S master.

audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);        
  1. Set volume.

audio.setVolume(21);   // 0...21   
  1. Play the song.

audio.connecttoFS(SD, songs[songIndex]);   
  1. Read the remaining time for the currently playing song.

int t = audio.getAudioFileDuration(); 
  1. Pause and play the song.

audio.pauseResume();   
  1. Stop playing the song.

udio.stopSong();    

**More Code Examples: ** img

9_IRremote

  1. Open the ā€œIRremoteā€ example code and upload it to eCar:
    img

  2. Result:
    Open the serial monitor of Arduino IDE, press the button on the infrared remote control, and the serial monitor prints different values.
    img
    img

Code analysis:

  1. Initialize the IR receiver.

IrReceiver.begin(irPin);    
  1. Determine whether infrared data is received.

if (IrReceiver.decode()){
    ...
}   
  1. The next infrared data is allowed to be received.

IrReceiver.resume();   

More information about IR receiver: Link
More information about IR remote control: Link

10_Motor

Principle of motor drive:
Four motors and two leds are controlled through the I2C communication protocol.
img

• The I2C speed is 100Khz, and the slave address is 0x2f.
• The PWM frequency between the motor and the LED is about 200Hz.
• Instruction format: 0x2f + device number + data.

  1. Device number: 0=Motor1, 1=Motor2, 2=motor3, 3=motor4.
    • data: 0-99, motors reverse speed, from small to large;
    • data: 100-199, motors forward speed, from small to large (mapped to 0-99).

  2. Device number: 4=LED1, 5=LED2.
    • data: 0-99, LEDs brightness, from small to large;

  3. Device number: 6=Reset.
    • data=1, reset the MCU.

More information about arduino I2C: Link
More information about I2C communication protocol: Link

Example:

  1. Open the ā€œmotorā€ example code and upload it to eCar:
    img

  2. Result:
    eCar keeps looping forward, stop, back, stop, turn left, stop, turn right, stop.

Code analysis:

  1. Initialize the I2C master.

i2cMotorInit();      
  1. Select the motor and set its rotation direction and speed.

// Description: Set motor speed and steering.
// Parameters:  Motor: 0-3 --> M1-M4
//              direction: 0=CCW, 1=CW  
//              speed: 0-99
void SetMotor(char motor, char direction, char speed){
  ...
} 

img

  1. eCar runs forward.

CarRunForword(carSpeed);    
  1. eCar runs backwards.

CarRunBack(carSpeed);       
  1. eCar turns left.

CarTurnLeft(carSpeed);        
  1. eCar turns right.

CarTurnRight(carSpeed);       
  1. eCar stop.

CarStop();       
  1. Set the brightness of 2 leds, parameter value: 0-99.

LedBrightness(99);        

11_WEB_app

  1. Open the ā€œweb_appā€ example code and upload it to eCar:
    img

  2. Close the mobile phone’s mobile network data, otherwise it may not enter the Web page control page!
    img

  3. The phone or tablet searches and connects to mCar’s wifi.
    img

Tip

After connecting to Wifi, your phone may pop up a window saying it cannot connect to the network, please ignore it!

  1. Open your phone or tablet’s browser and link to it by typing ā€œ192.168.4.1ā€ in the address bar.
    img

  2. The following screen should appear in your browser.
    img

  3. Result:
    Open the serial monitor of Arduino IDE, click or press the button on the Web App, and the serial monitor prints different values.
    img

Button

Serial port monitor display

Left display window

Left display window

Right display window

right display window

P: xxx%

P: 100%

F

F key is pressed!

B

B key is pressed!

L

L key is pressed!

R

R key is pressed!

P

P key is pressed!

Speed

Speed: xxx

Light

Light: xxx

Aservo

Aservo: xxx

Bservo

Bservo: xxx

Volt +/-

Volume: xx

<

Return key is clicked!

||<

Pause key is clicked!

>

Next key is clicked!

A

A key is clicked!

S

S key is clicked!

C

C key is clicked!

Code analysis:

  1. Set the working mode of ESP32, 1 is AP mode and 0 is Station mode.

bool ap = 1;    
  1. In AP (Access Point) mode, it sets its own Wifi name. In Station mode, set the name of the WiFi to connect to.

const char* ssid = "mCar";          
  1. In AP (Access Point) mode, it sets its own Wifi password. In Station mode, set the WiFi password to connect to. If password is empty, no password is required.

const char* password = "";   
  1. Enable http server function of ESP32 module.

startCarServer();        
  1. Let the three display windows of WEB_App display different strings.

displayWindow(0, "Left display window");
displayWindow(1, "right display window");
displayWindow(2, "P: 100%");      

12_eCar

  1. Open the ā€œeCarā€ example code and upload it to eCar:
    img

  2. Result:
    The function of this code is the same as the function of eCar out of the factory, which is also the integration of the above project function, so no more parsing code!

Control method: Link

Other Resources (option)


Arduino programming language

Arduino Getting Started Learning Kit


End!