How do I make pwm signals with arduino ide without useing analogwrite? - arduino

Hi can someone help with crating a pwm(or very similar) signal without analogwrite on the Arduino IDE because analogwrite wont work on the esp32.
And without using a library.
I need it to take values from 0 to 255.
I was thinking to use the second core one the esp32 but I have to control more than one pin.

you can create PWM pulse with delay or millis function. for example:
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delayMicroseconds(100);
digitalWrite(13, LOW);
delayMicroseconds(1000 - 100);
}
when you change the delay value, you can change duty-cycle level!

Related

how do i read and output pwm with a black pill board?

I am making an stm32 based motor speed controller for a DC motor. But I can't read PWM off my receiver and I can't make my MOSFET vary the output! I need help because now it's only on or off! I am using a RobotDyn BlackPill and an STP36NF06L Mosfet. And also I use Arduino ide with the STM board.
Code:
int Motor = PA15;
int rc = PB1;
int s;
void setup() {
pinMode(Motor, OUTPUT);
pinMode(rc, INPUT);
digitalWrite(Motor, LOW);
}
void loop() {
if(s = (map(pulseIn(rc, HIGH), 1100, 1900, 0, 255)) > 200) {
digitalWrite(Motor, HIGH);
}
else{
digitalWrite(Motor, LOW);
}
delay(10);
}
If you want to read an external PWM signal you can use a timer in input capture mode, such that one channel triggers on the rising edge and the other triggers on the falling edge then compute the period and as such the frequency and duty cycle of the incoming signal. It is well documented in the reference manual and there are various examples and even YouTube tutorials of input capture mode.

independent blinking lights in Arduino

I need to write a program in Arduino that turns ON or OFF two LEDs independently depending on the data received via serial communication. For example, the first LED will turn on if the Arduino board receives a1,and it will turn OFF if a2 is received. but I require help on doing this. i havent started on this yet but i am trying to do it using 2 blinking light code. I am new to Arduino programming and i don't understand some concepts. but i can definitely use help on doing this.
int LEDred = 3;
int LEDgreen = 6;
void setup() {
pinMode (LEDred, OUTPUT);
pinMode (LEDgreen, OUTPUT);
}
void loop() {
digitalWrite (LEDred, HIGH);
digitalWrite (LEDgreen, LOW);
delay (1000);
digitalWrite (LEDred, LOW);
digitalWrite (LEDgreen, HIGH);
delay (1000);
}
Use 1 or 2 instead of a1 and a2. Because it's easier to process.Consider you send 1 via serial port. Read the signal by
byte val;
if(Serial.available()>0){
val=Serial.read();
}
Then use another if statement in order to blink red or green LED

Arduino - Pixycam interferes with Blinky Example

I have a pixicam connected to an Arduino Uno. The pixi.init() method seems to be interfering with a normal blinky program. Here is the smallest reproducable program:
#include<SPI.h>
#include<Pixy.h>
Pixy pixy;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pixy.init();
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
If I comment out the pixi.init() line then the program works as normal (blinking on and off). When the line is not commented out, the light fails to blink.

Relay pin acts differently with load

I have the following code.
void setup(){
pinMode(14, OUTPUT);
digitalWrite(14, HIGH); //Relay
}
void loop(){
if (!digitalRead(14)){
digitalWrite(10,HIGH); //LED
digitalWrite(11,LOW); //LED
}else{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
}
In another portion of the code the relay pin changes states and I'm monitoring that with the digitalRead portion in the loop in hopes of changing which led is on based on the state.
Now the hard part. All of that works, except when I wire the relay to a magnet. This is all for a very intricate door control system with maglocks. And for some reason with the maglock hooked up to the relay the Arduino behaves very differently. It slows to a crawl once the relay is changed. Up until then all is fine, but as soon as the relay is activated, something causes it to slow way down.
What I can't figure out is why all is fine and relay triggers without side affects, until a load is attached to it.
Any ideas? Or a better way of monitoring a relay state? (Without storing its pseudo value in a variable)
You set the pin 14 as OUTPUT, but you're trying to read from it with digitalRead.
What you want to know is the value of the register that stores the value of the port.
You could go the easy way and use an auxiliar variable that stores the pin state like this:
bool state = true;
void setup(){
pinMode(14, OUTPUT);
digitalWrite(14, state); //Relay
}
void loop(){
if (!state){
digitalWrite(10,HIGH); //LED
digitalWrite(11,LOW); //LED
}else{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
}
And edit the rest of the code accordingly so state changes accordingly.
The 'Hardest to understand' solution, is to read the register value. since you're using pin 14 (the same as pin A0) you have to look into the port C According to the Arduino Reference on port manipulation (Link at the end of my answer).
So you can just do this:
void setup(){
pinMode(14, OUTPUT);
digitalWrite(14, HIGH); //Relay
}
void loop(){
if (!BitRead(PORTC,0)){ //Reads bit 0 of the register of PORTC (wich is the state of pin14)
digitalWrite(10,HIGH); //LED
digitalWrite(11,LOW); //LED
}else{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
}
This solution is more elegant and is exactly what you need. It might be harder to come up with so if you don't remember this in the future you could always use the "state" variable method.
Reference Bit Read Operation and Arduino Reference on port manipulation for more information.

Restarting Arduino and keeping the variables

I would like to restart the arduino board but keeping values of some variable. My solution would be calling setup() whenever I would like to restart. Something like this:
int led = 13;
int led2 = 50;
boolean restart = false;
void setup() {
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
if(!restart){
digitalWrite(led, LOW); // will only happen once
delay(3000);
}
digitalWrite(led, HIGH); // turn the LED on (and will be always on even after reset)
restart = true;
delay(3000);
digitalWrite(led2, HIGH); // indicate restart is called
delay(1000);
digitalWrite(led2, LOW);
setup();}
void loop() { }
I was thinking if this will cause any heavy usage in RAM. Or is there any better methods?
Thank you.
Use the EEPROM library. Have a button with an interrupt that saves the variable then read the variable in the setup() routine.
If you are getting or changing information slow enough you could constantly write the value but beware EEPROM on this chip is only certified to 100,000 writes per byte.

Resources