McLab12_MOSFET_PWM_3chLED_OS6_tk1 Hardware L432KC or similar with PWM capable outputs. The L432KC pin D13 for onboard LED is not PWM capable! LEDs and xxx Ohm resistor. For example for a 12 V supply, three LEDs in series select a 120 Ohm resistor for 20 mA LEDs. For testing 3 pieces MOSFET IRL540, LEDs, resistors.

Files at this revision

API Documentation at this revision

Comitter:
timo_k2
Date:
Tue Oct 05 12:14:27 2021 +0000
Parent:
0:d7a17d0b2bd3
Commit message:
Initial commit for the 3 LED circuit.

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/README.md	Fri Sep 04 18:40:54 2020 +0000
+++ b/README.md	Tue Oct 05 12:14:27 2021 +0000
@@ -1,3 +1,11 @@
+McLab12_MOSFET_PWM_3chLED_OS6_tk1
+Hardware
+L432KC or similar with PWM capable outputs.
+The L432KC pin D13 for onboard LED is not PWM capable!
+LEDs and xxx Ohm resistor. For example for a 12 V supply,
+three LEDs in series select a 120 Ohm resistor for 20 mA LEDs. 
+For testing 3 pieces MOSFET IRL540, LEDs, resistors. 
+
 PWM example for students to work with a MOSFET circuit. 
 
 ### License and contributions
--- a/main.cpp	Fri Sep 04 18:40:54 2020 +0000
+++ b/main.cpp	Tue Oct 05 12:14:27 2021 +0000
@@ -6,48 +6,83 @@
  * PWM example using the PwmOut API
  *
  *********************************************
- *Description: McLab12_MOSFET_PWM_OS6_tk1
+ *Description: McLab12_MOSFET_PWM_3chLED_OS6_tk1
  * Hardware
  * L432KC or similar with PWM capable outputs.
  * The L432KC pin D13 for onboard LED is not PWM capable!
- * LED and 220 Ohm resistor
- * For testing MOSFET: IRL540 + protection diode + DC motor
+ * LEDs and xxx Ohm resistor. For example for a 12 V supply,
+ * three LEDs in series select a 120 Ohm resistor for 20 mA LEDs. 
+ * For testing 3 pieces MOSFET IRL540, LEDs, resistors. 
  * Connect:
- * L432KC D10 -- 220 Ohm -- LED+ 
- * L432KC GND -- LED-
- * or
- * L432KC D10 -- MOSFET gate
- *
+ * L432KC VIN --- 5V from a 5 V regulator on 12 V supply.
+ * L432KC D3 -- MOSFET gate 
+ * L432KC D5 -- MOSFET gate
+ * L432KC D6 -- MOSFET gate
+ * 
+ * MOSFET drain -- resistor -- LEDs -- 12 V.
+ * MOSFET source -- GND
  * Operating system OS 6.0
- * Timo Karppinen 4.9.2020  Apache-2.0
+ * Timo Karppinen 5.10.2021  Apache-2.0
  */
 
 #include "mbed.h"
+#include <cmath>
 
-// Speed change every x seconds
-#define SpeedChanging_RATE     1s
+// change every x milliseconds
+#define changing_RATE     10ms
 
-// Initialise the digital pin D10 as a pwm output
+// Initialise the digital pins as a pwm output
 // The pin needs to be one of the PWM capable pins ! 
-PwmOut pwmMOSFET(D10);
+PwmOut pwmMOSFET1(D3);
+PwmOut pwmMOSFET2(D5);
+PwmOut pwmMOSFET3(D6);
 // Variables for the pwm
-int spCount = 1;
-float speed = 1.0;   // from 0.0 to 1.0 
+int intensCount1 = 1;
+int intensCount2 = 1;
+int intensCount3 = 1;
+float angle1 = 0.0;
+float angle2 = 0.0;
+float angle3 = 0.0;
+float intens1 = 1.0;   // from 0.0 to 1.0 
+float intens2 = 1.0; 
+float intens3 = 1.0; 
 float usT = 1000;   // f = 1000 Hz  T = 1 ms = 1000 us
                     // f = 10000 Hz  T = 100 us
 int main()
 {
-    pwmMOSFET.period_us(usT);
-    pwmMOSFET.write(1.0f);
+    pwmMOSFET1.period_us(usT);
+    pwmMOSFET2.period_us(usT);
+    pwmMOSFET3.period_us(usT);
+    pwmMOSFET1.write(1.0f);
+    pwmMOSFET2.write(1.0f);
+    pwmMOSFET3.write(1.0f);
+    
     while (true) {
+  
+        angle1 = angle1 + 0.1;
+        intens1 = 0.5 + 0.5 *sin(angle1);
+        
+        angle2 = angle2 + 0.08;
+        intens2 = 0.6 + 0.4 *sin(angle2);
         
-        spCount = spCount + 1;
-        if(spCount == 25){
-            spCount = 1;
+        angle3 = angle3 + 0.02;
+        intens3 = 0.7 + 0.3 *sin(angle3);
+        
+        if(angle1 > 628.31852){
+            angle1 = 0.0;
+            }
+        if(angle2 > 628.31852){
+            angle2 = 0.0;
             }
-        speed = 1/(float)spCount;
-        pwmMOSFET.write(speed);
+        if(angle3 > 628.31852){
+            angle3 = 0.0;
+            }
+        
         
-        ThisThread::sleep_for(SpeedChanging_RATE);
+        pwmMOSFET1.write(intens1);
+        pwmMOSFET2.write(intens2);
+        pwmMOSFET3.write(intens3);
+        
+        ThisThread::sleep_for(changing_RATE);
     }
 }
\ No newline at end of file