# Switchable PWM project

**URL:** <https://forum.arduino.cc/t/switchable-pwm-project/1122247>\
**Category:** General Electronics\
**Created:** [May 2, 2023, 10:59am UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247 "2023-05-02T10:59:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![regenerate3d](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/regenerate3d/32/904988_2.png) [@regenerate3d](https://forum.arduino.cc/u/regenerate3d)\
**Post date:** [May 2, 2023, 10:59am UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/1 "2023-05-02T10:59:24Z")

</div>

Hi All,

So I'm trying to figure out how to make a switchblade PWM device. I'll have a wireless Relay/Mosfet set up to tell Arduino to switch between momentary commands. This is to control a brushless speed controller with fixed PWM input. I ask for help as i really do struggle writing code because of poor learning retention. Electronics I'm ok with due to it being hands on. So if any kind person can direct me in the right way that would be great.

Simple diagram of idea below

Remote swith with three functions

Off function 0% PWN output Always default function unless one of two switches pressed.

Switch 1 Momentary 30% PWM

Switch 2 Momentary 60% PWM

So Two inputs and Three PWM out puts for Arduino.

continuous script until turned off.

I'm looking to either make a circuit myself of just embed a current Arduino device already available smallest form factor possible 32bit preferable if you have any advice.

thank you all for any time and advice you can give.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [May 2, 2023, 11:42am UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/2 "2023-05-02T11:42:38Z")

</div>

The PWM duty cycle can be adjusted by analogWrite() on any Arduino.

---

<div class="post-metadata">

**Author:** ![regenerate3d](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/regenerate3d/32/904988_2.png) [@regenerate3d](https://forum.arduino.cc/u/regenerate3d)\
**Post date:** [May 2, 2023, 11:47am UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/3 "2023-05-02T11:47:56Z")

</div>

Thank you Dr, The issue is i cannot comprehend how to write the code. i know i need to set pins for input and out put and PWM along with coding what pwm output for each switch etc. but I don't actually understand how that works. A switch in my hand is very east to understand but I don't understand how code works. sorry for being annoying but I'm being brutally honest as I'm struggling.

PWN level is governed by setting a number between 0 and 255 I believed I just don't understand how to implement it. and recommendation on a very small 32bit Arduino device?

Many thanks

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [May 2, 2023, 11:59am UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/4 "2023-05-02T11:59:23Z")

</div>

If you want to do this project yourself then start with the IDE examples on button input and PWM. Else there is a **Jobs and Paid Consultancy** forum section for turnkey solutions.

---

<div class="post-metadata">

**Author:** ![regenerate3d](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/regenerate3d/32/904988_2.png) [@regenerate3d](https://forum.arduino.cc/u/regenerate3d)\
**Post date:** [May 2, 2023, 12:15pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/5 "2023-05-02T12:15:18Z")

</div>

fantastic so this isn't a forum to seek help and advice!!!!

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [May 2, 2023, 12:31pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/6 "2023-05-02T12:31:51Z")

</div>

Did you read the sticky topics on top of this forum section?

I'm not here to do your homework for free. Stepping out...

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [May 2, 2023, 3:23pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/7 "2023-05-02T15:23:41Z")

</div>

Hello [regenerate3d](https://forum.arduino.cc/u/regenerate3d)

This is a nice project to get started.

Keep it simple and stupid firstly.  
Run some tutorials for the hardware selected.  
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

---

<div class="post-metadata">

**Author:** ![regenerate3d](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/regenerate3d/32/904988_2.png) [@regenerate3d](https://forum.arduino.cc/u/regenerate3d)\
**Post date:** [May 2, 2023, 3:53pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/8 "2023-05-02T15:53:35Z")

</div>

Thank you for replying. I've been working on this code for 6 months now, i have fairly severe learning issues related to reading and comprehension. That's why hands on is always better. i shall continue on and try and figure it out.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [May 2, 2023, 3:56pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/9 "2023-05-02T15:56:17Z")

</div>

What is the frequency of your PWM signal?

Which 32-bit Arduino Board you have -- is it ESP32?

---

<div class="post-metadata">

**Author:** ![regenerate3d](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/regenerate3d/32/904988_2.png) [@regenerate3d](https://forum.arduino.cc/u/regenerate3d)\
**Post date:** [May 2, 2023, 4:22pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/10 "2023-05-02T16:22:09Z")

</div>

Currently don't have a board but i need it to be as small as possible and 32bit for future proofing reasons.

This is the code I've come up with and it wont work

```plaintext
#include <Servo.h>

Servo PWM; // create servo object to control a servo

int SwitchLow = 2; // Pin designation for Switch Medium Low
int SwitchMed = 3; // Pin designation for Switch Medium Speed
int val; // variable to read the value from the analog pin

void setup() {
  PWM.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop1() {
  val = digitalRead(SwitchLow); // reads SwitchLow
  val = map(val, 0, 1, 0, 45); // Once Switch Low pressed Arduino will output 40% duty cycle of PWM
  PWM.write(val); // sets the PWM according to the scaled value
  delay(15); // waits 15ms
}
  void loop2() { 
  val = digitalRead(SwitchMed); // reads SwitchMed
  val = map(val, 0, 1, 0, 60); // Once Switch Med pressed Arduino will output 60% duty cycle of PWM
  PWM.write(val); // sets the PWM according to the scaled value
  delay(15); // waits 15ms
  }

```

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [May 2, 2023, 4:25pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/11 "2023-05-02T16:25:32Z")

</div>

Did you study the Compiler messages?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 2, 2023, 4:26pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/12 "2023-05-02T16:26:07Z")

</div>

> [@regenerate3d](#):
>
> it wont work

Your sketch does not have a loop() function in it so will not compile. Is that what you mean ?

---

<div class="post-metadata">

**Author:** ![regenerate3d](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/regenerate3d/32/904988_2.png) [@regenerate3d](https://forum.arduino.cc/u/regenerate3d)\
**Post date:** [May 2, 2023, 4:29pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/13 "2023-05-02T16:29:04Z")

</div>

honestly I haven't a clue I am still struggling with understanding how the code works. Because its not actually in my hands and its virtual I can't just swap and replace component till i get it right.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 2, 2023, 4:30pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/14 "2023-05-02T16:30:09Z")

</div>

Where did you get the code from ?

---

<div class="post-metadata">

**Author:** ![regenerate3d](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/regenerate3d/32/904988_2.png) [@regenerate3d](https://forum.arduino.cc/u/regenerate3d)\
**Post date:** [May 2, 2023, 4:30pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/15 "2023-05-02T16:30:32Z")

</div>

I don't know what that is. Due to learning issues I am unable to retain information. it's almost like memory loss

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [May 2, 2023, 4:31pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/16 "2023-05-02T16:31:09Z")

</div>

> [@regenerate3d](#):
>
> and replace component till i get it right.

The compiler delivers messages for this.

---

<div class="post-metadata">

**Author:** ![regenerate3d](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/regenerate3d/32/904988_2.png) [@regenerate3d](https://forum.arduino.cc/u/regenerate3d)\
**Post date:** [May 2, 2023, 4:32pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/17 "2023-05-02T16:32:00Z")

</div>

just used the servo example and then tried to slice bit i kind of know to get it to work. one of the loops works on its own but when I add a second switch i falls apart. I'm using tinkercad Arduino simulator

---

<div class="post-metadata">

**Author:** ![regenerate3d](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/regenerate3d/32/904988_2.png) [@regenerate3d](https://forum.arduino.cc/u/regenerate3d)\
**Post date:** [May 2, 2023, 4:32pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/18 "2023-05-02T16:32:37Z")

</div>

26:1: error: 'Loop' does not name a type; did you mean 'loop'?

is returned on simulator

---

<div class="post-metadata">

**Author:** ![regenerate3d](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/regenerate3d/32/904988_2.png) [@regenerate3d](https://forum.arduino.cc/u/regenerate3d)\
**Post date:** [May 2, 2023, 4:33pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/19 "2023-05-02T16:33:43Z")

</div>

In function `main': 46: undefined reference to `loop'  
error: ld returned 1 exit status

sorry this is returned the other one was a previous issue.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 2, 2023, 4:34pm UTC](https://forum.arduino.cc/t/switchable-pwm-project/1122247/20 "2023-05-02T16:34:16Z")

</div>

That message did not come from the sketch that you posted

Which simulator are you using ?

[Next page](https://forum.arduino.cc/t/switchable-pwm-project/1122247.md?page=2)
