I've read that Vin pin is directly connected to usb cable in esp8266 (which is 5V). I wonder if I can power a relay 5V and a servo motor Sg90 with this pin? And is there any danger of doing that ?
The GPIO pins on the ESP8266 (including the ones with built-in NodeMCU) are 3.3v tolerant.
First, if there is ever any feedback from the 5v+ from the relay coming back down the signal wire, it'll likely cook the ESP.
Also, if it's a 5v relay, 3.3v from the ESP most likely wouldn't be enough to trigger the unit anyhow.
You'll either need to power the relay from an external power supply (or the Vin pin depending on the mA the relay requires), and then use a voltage converter (eg. logic level converter) so you can have 5v on the relay side (I'm speaking of the signal pin here), and 3.3v on the ESP side, or purchase a relay that will operate on 3.3v.
(Answering in more detail, as this is the top search hit for "ESP8266 Vin", at least for me)
Vin is NOT directly connected to USB power. There is a protection diode between USB+/VU and
Vin, which has a limited current capacity.
If your board provides
VU, that one is directly connected to USB power.
As long as you only power 1-2 tiny devices, it should be OK. (controlling will need additional components, or it will fry your ESP)
Since you talk about 'Vin' and '5V', I'm going to assume you have a devboard, not a 'naked' ESP8266. If your "esp8266" has a USB-connector and flash/reset buttons, then its a devboard-version. If it is only a fingernail-sized metal cover with a 1mm edge with solderpads, then its 'naked'.
Vin pin is a power input for the onboard regulator.
On all common devboards, there is a protection diode between the USB+ wire, and
Some devboards also provide a
VU-pin, which is directly connected to the USB+ wire without anything in-between.
The 3.3V regulator converts any input voltage (5V-19V) connected to
Vin into the 3.3V needed by the ESP chip and the
3V3-pins. The regulator has capacity (barely) for the ESP and a (large) handful of leds, not much more.
There is always danger when connecting mechanical/magnetic 'power'-devices (relays, servos and motors) in the same circuit with 'sensitive' microcontrollers/logic gates. 'power' devices are noisy and cause feedback which must be filtered, blocked and absorbed so it will not fry 'small' electronics. (even a toothbrush motor is 'power', compared to microchips). 'Power' devices are also hungry, causing voltage drops when activated. These drops can cause glitches, bugs and even resets in logic-level electronics.
This not stop many people from trying it anyway, and it will often work, for a while, until it someday 'mysteriously' stops working because the chip finally gave out under the repeated abuse.
(side note: as other have mentioned: You also cannot control magnetic/mechanical devices directly from the signal pins. Microcontroller outputs are too weak. Trying anyway can destroy your microcontroller with the feedback.)
A detailed background explanation for powering ESP devboards, including wiring schematics and explanations, can be found here: https://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/powering-the-esp-12e-nodemcu-development-board/
Relays, motors, and other inductive loads release a surge of reverse current when you remove power from them. You have to connect a suitably sized “flyback diode” across the coil, wired backwards from the normal current flow direction to protect the other devices on the circuit from that reverse current or they will likely be damaged.
Relays and motors also draw a large in-rush of current when they are first connected, which can cause the supply voltage to droop.
You can buy relays that are made to be controlled by logic levels. They take a low power 5V or 3.3V logic level input, plus a separate power supply, and use the logic input to switch the relay on and off safely. The relay modules I’m using have optical isolation for the logic lines for extra protection.