In this tutorial, I will show you how to build a simple Wemos NodeMCU, or other ESP-based, car system. Over the course of it, I will use the term Arduino for this device.
Before starting the course, I suggest you read the articles “quick start” for both the website and Arduino.
Parts- listed above, make sure to read comments next to the part
We start with assembling the robot chassis. Engines on the right and left side are connected to each other and we supply the power cable to both at once. It is important that after powering up the engines spin in the same direction, the wheels on the right side always spin in the same direction, the same on the left side of the car.
The whole looks like this:
When the left side of the robot is spinning at a different speed than the right one then the robot turns. When the right side is spinning at the same speed but in the opposite direction as the right one, then the robot spins around its own axis
ElectronicsAt the beginning do not hook the converter, bridge and wemos. Only after setting the inverter, check that the voltage is exactly 5v on the board.
Instead of looking at the connection diagram of the bridge, it is better to check with the list:
- Arduino D7 – AIN1 bridge
- Arduino D6 – AIN2 bridge
- Arduino D5 – PWMA bridge
- Arduino D3 – BIN1 bridge
- Arduino D2 – BIN2 bridge
- Arduino D1 – PWMB bridge
- Arduino 5V connected to the bus with 5V voltage from the converter
- Arduino 3.3V connected to the bus with 3.3V voltage (the arduino stabilizer provides us with voltage on this bus)
- StandBy bridge – 3.3V bus
- VCC bridge – 3.3V bus
- VM bridge – directly to the battery – this is the voltage transmitted to the motors
- All wemos masses, bridge, grounding converter
- inverter input – battery plus to plus minus to minus;)
- output transducer – 5V bus
light effects:
- connect two stripes together to get one long
- GND LED strip – ground
- LED strip power supply 5V
- HV1 converter output – signal input for the LED strip
- LV1 – Arduino D8 input
- resistor 6.8k ground – LED strip signal input
The H-bridge controls the motors for inputs (AIN1, AIN2) depending on the state determines the direction of the engine’s spinning, and PWM signal is sent to the PWMA input, the higher the signal fill, the faster the engine will run
arduino.ino:
#define AIN1 D7
#define AIN2 D6
#define PWMA D5
#define BIN1 D3
#define BIN2 D2
#define PWMB D1
void setupPins() {
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
analogWriteFreq(1000);
analogWriteRange(1023);
}
Above setting of outputs. and frequency setting analogWriteFreq (1000); (as 100 will be the engines will “jump”).
And the function for motor control is given as an argument from -1023 to 1023 and the function will control the motor accordingly
arduino.ino:
void motorA(int speed) {
uint8_t mode=0;
if (speed<0){
mode=1;
speed=-speed;
}else if (speed>0){
mode=2;
}else{
speed=0;
mode=0;
}
if (mode==0) {
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
}else if (mode==1){
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
}else if (mode == 2) {
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
}
analogWrite(PWMA, speed);
}
The same function is for the second engine.
For lighting, we will use the Adafruit_NeoPixel library – it should be added to Arduino:
and the SingleRing object – which displays the corresponding effects more about the class here
Arduino Programwe download the entire directory from here to our computer to the arduino directory. Then we complete the constants:
arduino.ino:
#define WIFI_NAME ""
#define WIFI_PASSWORD ""
#define DEVICE_ID 1001
#define DEVICE_NAME "arduino car"
#define TOKEN ""
More about constants and how to supplement them here. Of course, before uploading and completing the token, you must have an account at remoteme.org. After uploading, our arduino will appear on our device manager.
I have described the fragment of the arduino code regarding the remoteme.org system in the quick start
Car TestingBefore testing, put the car in such a way that it is stable and none of the wheels touches the ground!
arduino.ino:
void onUserMessage(uint16_t senderDeviceId, uint16_t dataSize, uint8_t *data) {
uint16_t pos=0;
uint8_t type=RemoteMeMessagesUtils::getUint8(data, pos);
if (type==1){//motor
motorA(RemoteMeMessagesUtils::getInt16(data, pos));
motorB(RemoteMeMessagesUtils::getInt16(data, pos));
}else if (type==2){//light
top.setMode(RemoteMeMessagesUtils::getInt16(data, pos));
}
}
When the message arrives, it is directed to this function where:
If the first byte is 1 = motor control mode
- the next two bytes are the speed of the left motor (-1023 – 1023) – represented as a two-byte int
- the next two bytes are the speed of the right motor (-1023 – 1023) – represented as a two-byte int
If the first byte is 2 = light effect
- the next two bytes are the effect to display
to test engine operation, send a message to the car. Click “
” At the device and we get a window to send messages, fill in as on the screen
the most important are bytes to send “1 0 0 0 0” means to set the speed of the car to zero (enter these values in the field “Hex” if you enter in the Text field will be sent messages that adruino will not be able to decode)
if we enter in Hex: “1 4 FF 1 AA”
it will mean:
- 1 – control the motors
- 4 0 = 256 * 3 + FF = 256 * 3 + 15 * 16 + 15 = 1023 – engine speed one (max)
- 1 AA = 256 * 1 + 10 * 16 + 10 = 426 – the speed of the second engine
PAfter sending one of the engines will be spinning at the maximum speed and the other will be more than half the slower. Of course, to stop the engines we send 1 0 0 0 0;
Play with the control of lights on your own.
Of course, we will not control the car in this way. we will make a website that will send similar messages.
Websitehow to create a website here. We create a blank page:
and then drag one file after another from here
on the field “Drop new files here”, after this operation all files will be on us in the cloud:
Opening the page on the mobile phone: Click on index.html then Get anymous link (more in the quick web page)
We have 3 options: rewrite this long URL to a mobile phone, 2 copy and send to a mobile phone, 3 or generate a QR code and scan the phone.
Of course, we can also open the link in the browser (chrome) and after switching to the developer tool (F12) and go into the mobile phone mode Ctrl + Shift + M. And test it
Control: putting a finger on the screen the wheel changes positions, from this moment the swing is measured from the place where the finger is placed, moving the finger on the screen we control the car. And the red stripes on the sides show the speed of the left and right engines:
About the connection buttons (those green at the top) more in the quick start
To change the light effect, select the effect from the drop-down menu at the bottom left.
When the car is driving differently than we expect – we change the engine connection to the H-bridge. Or modify the script.js code:
function sendMotor(){
var ret = new RemoteMeData(5);
ret.putInt8(1);
ret.putInt16(fasterStart(/*Y*/carController.getLeftSideSpeed())*1024);//L1
ret.putInt16(fasterStart(/*X*/carController.getRightSideSpeed())*1024 );//L2
console.info(ret)
remoteme.sendUserMessageByFasterChannel(arduinoDeviceId,ret);
}
by changing the position lines L1 and L2 – and in places X and / or Y add minus.
It is also a function that sends data to the car – identically as we sent using the remota.org GUI
- carController.getLeftSideSpeed () – returns the number s of the range [-1 1]
- fasterStart – makes the small deflection a little bigger, so that the car can move faster
- ret.putInt8 (1); – the first byte of the message informs arduino that we want to control the engine
- ret.putInt16 – write the motor speed as a two-byte integer to the message
- remoteme.sendUserMessageByFasterChannel (arduinoDeviceId, ret); – we send a message to arduino.
arduinoDeviceId is taken from index.html
index.html:
<script>
var thisDeviceId=####deviceId#;
var arduinoDeviceId=####arduinoDeviceId#;
</script>
#### arduinoDeviceId # – is replaced by remoteme while serving to the browser into device id the first arduino – and that we have only one will be our arduino steering the car. When we have more arduino we give here the deviceId of arduino to which we want to send the message
there may also be one fragment in script.js that is not clear
script.js:
function onMove(x,y){
...
ot.execute(sendMotor);
}
and ot object initialization
script.js:
function setup(){
ot = new OperationTimer(100);
it means that the sendMotor function will not be called more often than 100ms – protection against the clogging of our arduino if we would move the finger long and fast, more in the documentation
That’s it, I was not describing the code line by line thinking that for someone with basic knowledge about javascript and programming arduino it will be clear.
As always, I recommend fanpage on FB https://www.facebook.com/remoteMe.org/ where I throw information about the project, future courses.
Regards,
Maciek
Comments