Advertisement
microrobotics

BS170 MOSFET

Apr 26th, 2023
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The BS170 is an N-Channel MOSFET that can be used to control various loads, such as LEDs or motors, using an Arduino. Here's an example code that demonstrates how to control an LED using a BS170 MOSFET and an Arduino. The LED will fade in and out using pulse-width modulation (PWM).
  3.  
  4. Hardware Setup:
  5.  
  6. Connect the Gate of the BS170 MOSFET to digital pin 9 on the Arduino.
  7. Connect the Source of the BS170 MOSFET to the GND (ground) of the Arduino.
  8. Connect the Drain of the BS170 MOSFET to the cathode (shorter leg) of the LED.
  9. Connect the anode (longer leg) of the LED to a current-limiting resistor (e.g., 220 ohms).
  10. Connect the other end of the resistor to the Vcc (5V) of the Arduino.
  11.  
  12. This code fades in and out the LED connected to the BS170 MOSFET using the Arduino's pulse-width modulation (PWM) feature. The Gate of the MOSFET is connected to digital pin 9, which supports PWM. The analogWrite function is used to control the PWM signal, varying the brightness of the LED.
  13.  
  14. Code:
  15. */
  16.  
  17. const int ledPin = 9; // Digital pin 9 connected to the Gate of the BS170 MOSFET
  18.  
  19. void setup() {
  20.   pinMode(ledPin, OUTPUT); // Set the LED pin as an output
  21. }
  22.  
  23. void loop() {
  24.   // Fade in the LED
  25.   for (int i = 0; i <= 255; i++) {
  26.     analogWrite(ledPin, i); // Set the PWM value of the LED pin
  27.     delay(10); // Wait for 10 milliseconds
  28.   }
  29.  
  30.   // Fade out the LED
  31.   for (int i = 255; i >= 0; i--) {
  32.     analogWrite(ledPin, i); // Set the PWM value of the LED pin
  33.     delay(10); // Wait for 10 milliseconds
  34.   }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement